📜  Julia 中的正则表达式

📅  最后修改于: 2022-05-13 01:55:04.467000             🧑  作者: Mango

Julia 中的正则表达式

Julia 具有由PCRE 库提供的与Perl 兼容的正则表达式(正则表达式)。正则表达式可用于在字符串中查找正则模式,而在 Julia 中,正则表达式本身作为字符串输入,这些字符串被解析为可用于有效搜索字符串中的模式的状态机。

在 Julia 中,使用以 r 开头的各种标识符作为前缀的非标准字符串字面量输入正则表达式。没有打开任何选项的最基本的正则表达式字面量只使用 r"..."。 r"..."字面量是在没有插值和转义的情况下构造的。

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


Julia
# 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))


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


Regex 

在编写正则表达式时使用不同的字符(正则表达式标记):

 TokenDescription
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.

使用正则表达式进行模式匹配

match()是 Julia 中的一个内置函数,用于在指定字符串中搜索给定正则表达式的第一个匹配项。

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.

朱莉娅

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

Regex() 构造函数的使用

Regex()构造函数可用于以编程方式创建有效的正则表达式字符串。这允许在构造正则表达式字符串时使用字符串变量的内容和其他字符串操作。

朱莉娅

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