首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >"r“在”.“之前的目的是什么?在DataFrames.jl中

"r“在”.“之前的目的是什么?在DataFrames.jl中
EN

Stack Overflow用户
提问于 2022-01-21 01:22:43
回答 2查看 114关注 0票数 2

我注意到人们使用r".."。它是用来做什么的?谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-01-21 06:36:46

@cbk为您很好地概述了朱莉娅中r"..."正则表达式的用法。

在DataFrames.jl中,您可以使用正则表达式作为列选择器。下面是一些示例,在这些示例中,r"b"匹配所有包含"b"的列:

代码语言:javascript
复制
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
票数 4
EN

Stack Overflow用户

发布于 2022-01-21 01:35:48

r"..."是定义正则表达式的朱莉娅语法,每当需要regexp时,整个语言(不仅仅是数据帧)都使用它。通过在Julia的内置帮助中搜索r"",您可以找到有关此语法的更多信息:

代码语言:javascript
复制
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对象,这些对象以后可以应用于一个或多个字符串,其中包含matchreplace等函数。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70795299

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档