How to use Regex() function?

r"(?<=NH3\)\[)\w+(?=\])" is ok but Regex("(?<=NH3\)\[)\w+(?=\])") reports

ERROR: LoadError: syntax: invalid escape sequence

why?

OK. just figured out… Need to use \\ instead of \ . The documentation should have mentioned that …

The string macro form doesn’t recognize \ as an escape character, it’s just another character in the regex (which has its own escape functionality according to regex). But in normal Julia strings, if you use \ it needs to be part of an escape sequence such as \n (newline) or \\ (literal backslash). If you want to use the Regex constructor, you can use Regex(raw"my_string \ with weird escapes").

2 Likes

https://docs.julialang.org/en/v1/manual/metaprogramming/#man-macros

it does by showing you you need \\ if you’re constructing via Regex()

Cool. Maybe it should also be in ?help

This is not what ?help does (try it out).