Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toggle regex options inside a named capture

Tags:

regex

ruby

In a regular expression in Ruby you can name a capture using the syntax:

/(?<name>…)/

and you can toggle the m/i/x options on/off within a non-capturing group using the syntax:

/(?i:[a-z])/

Is there any way to combine the two in a single group?


All of these work, but I am wondering if there is a simpler option:

/(?i:(?<name>…))/      # case-insensitive just within the region
/(?<name>(?i:…))/      # case-insensitive just within the region
/(?i)(?<name>…)(?-i)/  # case-insensitive turned on, then turned off
like image 558
Phrogz Avatar asked Mar 25 '26 12:03

Phrogz


1 Answers

you can do that too:

/(?<name>(?i)...)/

AFIK, that's all!

like image 195
Casimir et Hippolyte Avatar answered Mar 27 '26 05:03

Casimir et Hippolyte



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!