Open In App

Pattern Matching in Julia

Last Updated : 25 Aug, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Using match() Function

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"))


Using ismatch() Function

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"))


Using matchall() Function

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"))


Using eachmatch() Function

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




Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads