Pattern Matching in Julia

25 Aug, 2020
Pattern matching is the process of checking whether a specific sequence of characters/tokens/data exists among the given data. Regular programming languages make use of regular expressions (regex) for pattern matching. 
In Julia to match given string with a given pattern following pre-defined functions can be used:
match()
ismatch()
matchall()
eachmatch()
All of these functions take arguments in needle, haystack order. In the context of regexes, the regex is the needle, and the text is the haystack.
match() is an inbuilt function in Julia which is used to search for the first match of the given regular expression in the specified string. If a regular expression does match, the value returned by match is a RegexMatch object. These objects record how the expression matches, including the substring that the pattern matches and any captured substrings, if there are any. 
You can extract the following info from a RegexMatch object:
The entire substring matched: m.match
The captured substrings as an array of strings: m.captures
The offset at which the whole match begins: m.offset
The offsets of the captured substrings as a vector: m.offsets
Syntax:
match(r::Regex, s::AbstractString, idx::Integer)

Parameters:
r::Regex: Specified regular expression.
s::AbstractString: Specified string.
idx::Integer: It specifies the point from which the searching get started.

Returns: It returns a RegexMatch object containing the match or nothing if the match failed.

Julia
println(match(r"s.t", "the cat sat on the mat"))println(match(r"^\w+", "This is a test"))
The ismatch() function returns a boolean indicating whether a match was found inside the string.
Syntax:
match(r::Regex, s::AbstractString)

Parameters:
r::Regex: Specified regular expression.
s::AbstractString: Specified string.

Returns: It returns a boolean value and returns true if match is found, otherwise false.

Julia
println(ismatch(r"(cat|dog)s?", "My pigs"))println(ismatch(r"(cat|dog)s?", "My cats"))
The matchall() function can be used to find all matches of a regular expression in a string.
Syntax:
matchall(r::Regex, s::AbstractString, overlap::Bool=false)

Parameters:
r::Regex: Specified regular expression.
s::AbstractString: Specified string.
overlap: Boolean Value. If overlap is true, the matching sequences are allowed to overlap indices in the original string, otherwise they must be from distinct character ranges.

Returns: Return a vector of the matching substrings from eachmatch.

Julia
println(matchall(r"[a-z]", "is a letter"))
The eachmatch() function returns an iterator over RegexMatch objects, suitable for use with for loops.
Syntax:
eachmatch(r::Regex, s::AbstractString, overlap::Bool=false)

Parameters:
r::Regex: Specified regular expression.
s::AbstractString: Specified string.
overlap: Boolean Value. If it is true, the matching sequences are allowed to overlap indices in the original string, otherwise they must be from distinct character ranges.

Returns: Search for all matches of a the regular expression r in s and return a iterator over the matches. 

Julia
for m in eachmatch(r"(cat|dog)s?", "My cats and my dog")    println("Matched $(m.match) at index $(m.offset)")end
Julia
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Vectors in Julia
Getting rounded value of a number in Julia - round() Method
Storing Output on a File in Julia
Reshaping array dimensions in Julia | Array reshape() Method
Manipulating matrices in Julia
Exception handling in Julia
Formatting of Strings in Julia
Tuples in Julia
while loop in Julia
Creating array with repeated elements in Julia - repeat() Method