Libor P
Libor P

Reputation: 89

Julia regular expression \w+/g

I'm using replace function with regular expression to find all non alphanumeric characters in string.

new = replace(cont, r"\w+/g" => "")

However, above code do nothing with a string.

If I remove "/g" it works, and it removes all words.

Upvotes: 2

Views: 132

Answers (1)

Bogumił Kamiński
Bogumił Kamiński

Reputation: 69869

The /g part is not needed because by default replace replaces all matches of a pattern. If you wanted to e.g. replace one match pass count=1 keyword argument to replace.

Now, in most regular expression parsers /g would be an invalid sequence, but in Julia it is accepted, and just matches /g verbatim, see:

julia> match(r"\w+/g", "##abc/gab##")
RegexMatch("abc/g")

julia> replace("##abc/gab##", r"\w+/g" => "")
"##ab##"

as is explained here the /g flag in e.g. Perl can be found at the end of regular expression constructs, but is not a generic regular expression flag, but applies to the operation being performed.

In Julia the allowed flags for regex are listed here and they are i, m, s, and x and are suffixed after the regular expression, e.g. r"a+.*b+.*?d$"ism.

Upvotes: 4

Related Questions