How to use a flag in Regex()?


I wan’t to use a variable interpolation and a regex flag at the same time, is it possible ? For example

julia> x="StRinG"

julia> Regex("$x")

# if I wan to ignore to ignore case with a flag :

julia> Regex("$x"i)
ERROR: UndefVarError: i not defined
 [1] top-level scope at REPL[34]:1

julia> r"StRinG"i # works but no string interpolation is possible

julia> Regex("(?i)$x") # also works in that case but the flag is part of the regex

You can give "i" as the second argument to Regex, as the documentation indicates:

help?> Regex
search: Regex RegexMatch

  Regex(pattern[, flags])

  A type representing a regular expression. Regex objects can be used to
  match strings with match.

  Regex objects can be created using the @r_str string macro. The
  Regex(pattern[, flags]) constructor is usually used if the pattern string
  needs to be interpolated. See the documentation of the string macro for
  details on flags.

julia> Regex("StRinG", "i")
1 Like

Thank you @pfitzseb ! I read the documentation here and this second argument was only mentioned for r" ".