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.
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.
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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.