Skip to content
Related Articles

Related Articles

Improve Article
Regular Expressions in Julia
  • Difficulty Level : Medium
  • Last Updated : 25 Aug, 2020

Julia has Perl-compatible regular expressions (regexes), as provided by the PCRE library. Regular expressions can be used to find regular patterns in strings and in Julia regular expressions are themselves input as strings which are parsed into a state machine that can be used to efficiently search for patterns in strings. 

In Julia, regular expressions are input using non-standard string literals prefixed with various identifiers beginning with r. The most basic regular expression literal without any options turned on just uses r”…”. The r”…” literal is constructed without interpolation and unescaping.


myregex = r"^\s*(?:#|$)" 

Use of different characters(regex tokens) while writing a Regular Expression :

A backslash always escapes the character that follows it. The escaped character can be a single character or the start or end of a range. But Alphanumeric characters cannot be escaped with a backslash.
^A caret (^) negates the character class if you place it immediately after the opening bracket. It makes the character class match any character that is not in the list.
\rused for carriage return. 
\nused for line feed.
It creates a range when it is placed between two characters. The range includes the character before the hyphen, the character after the hyphen, and all characters that lie between them in numerical order. ‹[A-z]› includes all characters in the ASCII table between the uppercase A and the lowercase z. The range includes some punctuation, so ‹[A-Z\[\\\]\^_`a-z]› matches the same characters more explicitly. 
\bMatches word boundaries. 
\BMatches non-word boundaries.
\dAny character that matches decimal digit.
\sAny kind of whitespace or invisible separator.
\wAny character that matches an alphanumeric character, plus underscore.

Pattern Matching using regex

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.

match(r::Regex, s::AbstractString, idx::Integer)

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.


# example 1:
myregex = r"^\s*(?:#|$)" 
println(match(myregex, "# a comment"))
# example 2:
println(match(myregex, "not a comment"))
# example 3:
email_pattern = r".+@.+"
input = ""
println(match(email_pattern, input))

Use of Regex() Constructor

The Regex() constructor may be used to create a valid regex string programmatically. This permits using the contents of string variables and other string operations when constructing the regex string.


name = "Jon"
regex_name = Regex("[\"( ]$name[\") ]"# interpolate value of name
println(match(regex_name, " Jon "))
println(match(regex_name, "[Jon]") === nothing)

My Personal Notes arrow_drop_up
Recommended Articles
Page :