Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of "r" before "..." in DataFrames.jl

I noticed people using r"..". What is it for? thanks

like image 426
siebenstein Avatar asked Feb 01 '26 02:02

siebenstein


1 Answers

r"..." is Julia syntax for defining a regular expression, and is used throughout the language (not just in data frames) whenever a regexp is needed. You can find more information about this syntax by searching for r"" in the Julia REPL's built-in help:

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.

More broadly, the pattern of some word or letter immediately preceding / juxtaposed with a quotation is called a string macro (or non-standard string literal) and you can even define your own (as in packages like this). The r"..."syntax is one that just happens to be built-in and is used specifically for definining regexp objects that can later be applied to one or more strings with functions such as match and replace.

like image 161
cbk Avatar answered Feb 03 '26 07:02

cbk