Do a Nim regex modifier just like Perl one

Viewed 73

How is Nim regex PCRE modifier done as in Perl itself is e.g.

/foobar/i
/ foo bar /x    
/foobar.*/s

etc ?

Please explain correctly doing such on Nim
Thanks much in advance

2 Answers

I report the excellent answer provided by Hlaftaana to this same question on the forum: https://forum.nim-lang.org/t/8884

With re you supply a set of RegexFlag arguments to the re function, with nre you supply them inside the regex.

Example:

import re
let reg1 = re("foo bar", {reIgnoreCase, reExtended})
# or
import nre
let reg2 = re"(?ix)foo bar"

You should be using inline modifiers,

(?i)foobar
(?x)foobar
(?s)foobar

Or, modifier groups, which is especially handy when only a part of the pattern needs to be modified:

(?i:foobar)
(?x:foobar)
(?s:foobar)

See "Regex Modifiers—Turning them On".

Related