Julia Language Regexes Finding matches


There are four primary useful functions for regular expressions, all of which take arguments in needle, haystack order. The terminology "needle" and "haystack" come from the English idiom "finding a needle in a haystack". In the context of regexes, the regex is the needle, and the text is the haystack.

The match function can be used to find the first match in a string:

julia> match(r"(cat|dog)s?", "my cats are dogs")
RegexMatch("cats", 1="cat")

The matchall function can be used to find all matches of a regular expression in a string:

julia> matchall(r"(cat|dog)s?", "The cat jumped over the dogs.")
2-element Array{SubString{String},1}:

The ismatch function returns a boolean indicating whether a match was found inside the string:

julia> ismatch(r"(cat|dog)s?", "My pigs")

julia> ismatch(r"(cat|dog)s?", "My cats")

The eachmatch function returns an iterator over RegexMatch objects, suitable for use with for loops:

julia> for m in eachmatch(r"(cat|dog)s?", "My cats and my dog")
           println("Matched $(m.match) at index $(m.offset)")
Matched cats at index 4
Matched dog at index 16