我注意到人们使用r".."。它是用来做什么的?谢谢
发布于 2022-01-21 06:36:46
@cbk为您很好地概述了朱莉娅中r"..."正则表达式的用法。
在DataFrames.jl中,您可以使用正则表达式作为列选择器。下面是一些示例,在这些示例中,r"b"匹配所有包含"b"的列:
julia> using DataFrames
julia> df = DataFrame(a=1, b1=2, b2=3, c=4)
1×4 DataFrame
Row │ a b1 b2 c
│ Int64 Int64 Int64 Int64
─────┼────────────────────────────
1 │ 1 2 3 4
julia> df[:, r"b"] # data frame indexing
1×2 DataFrame
Row │ b1 b2
│ Int64 Int64
─────┼──────────────
1 │ 2 3
julia> select(df, r"b") # selection operation
1×2 DataFrame
Row │ b1 b2
│ Int64 Int64
─────┼──────────────
1 │ 2 3
julia> combine(df, AsTable(r"b") => ByRow(sum)) # rowwise aggregation of selected columns
1×1 DataFrame
Row │ b1_b2_sum
│ Int64
─────┼───────────
1 │ 5发布于 2022-01-21 01:35:48
r"..."是定义正则表达式的朱莉娅语法,每当需要regexp时,整个语言(不仅仅是数据帧)都使用它。通过在Julia的内置帮助中搜索r"",您可以找到有关此语法的更多信息:
help?> r""
@r_str -> Regex
Construct a regex, such as r"^[a-z]*$", without interpolation and unescaping (except
for quotation mark " which still has to be escaped). The regex also accepts one or
more flags, listed after the ending quote, to change its behaviour:
• i enables case-insensitive matching
• m treats the ^ and $ tokens as matching the start and end of individual
lines, as opposed to the whole string.
• s allows the . modifier to match newlines.
• x enables "comment mode": whitespace is enabled except when escaped with \,
and # is treated as starting a comment.
• a disables UCP mode (enables ASCII mode). By default \B, \b, \D, \d, \S, \s,
\W, \w, etc. match based on Unicode character properties. With this option,
these sequences only match ASCII characters.
See Regex if interpolation is needed.
Examples
≡≡≡≡≡≡≡≡≡≡
julia> match(r"a+.*b+.*?d$"ism, "Goodbye,\nOh, angry,\nBad world\n")
RegexMatch("angry,\nBad world")
This regex has the first three flags enabled.更广泛地说,紧跟引号/并排的单词或字母的模式称为https://docs.julialang.org/en/v1/manual/metaprogramming/#meta-non-standard-string-literals (或非标准字符串文字),您甚至可以定义自己的(如这之类的包)。r"..."语法恰好是内置的,专门用于定义regexp对象,这些对象以后可以应用于一个或多个字符串,其中包含match和replace等函数。
https://stackoverflow.com/questions/70795299
复制相似问题