Skip to content
Related Articles

Related Articles

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.

Julia

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Regex 

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

 Token Description
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.
\r used for carriage return. 
\n used 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. 
\b Matches word boundaries. 
\B Matches non-word boundaries.
\d Any character that matches decimal digit.
\s Any kind of whitespace or invisible separator.
\w Any 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.



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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.

Julia

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


My Personal Notes arrow_drop_up
Recommended Articles
Page :