Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the o modifier for a regexp mean?

Tags:

regex

ruby

Ruby regexp has some options (e.g. i, x, m, o). i means ignore case, for instance.

What does the o option mean? In ri Regexp, it says o means to perform #{} interpolation only once. But when I do this:

a = 'one'  
b = /#{a}/  
a = 'two'  

b does not change (it stays /one/). What am I missing?

like image 574
Liao Pengyu Avatar asked Nov 11 '12 19:11

Liao Pengyu


People also ask

What are regex modifiers?

There are three basic modifiers, or flags, for RegExp, they are i, g, m. The i modifier is used when you want to perform a case-insensitive search. Case-insensitive means search all words and ignoring the capitalization of letters. The g modifier is used when searching “globally” in your code.

What does o mean in Perl?

This is an optimization in the case that the regex includes a variable reference. It indicates that the regex does not change even though it has a variable within it. This allows for optimizations that would not be possible otherwise.

What does regex 0 * 1 * 0 * 1 * Mean?

Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1. 1* means any number of ones.

What are regex symbols?

A regular expression (shortened as regex or regexp; sometimes referred to as rational expression) is a sequence of characters that specifies a search pattern in text. Usually such patterns are used by string-searching algorithms for "find" or "find and replace" operations on strings, or for input validation.


1 Answers

Straight from the go-to source for regular expressions:

/o causes any #{...} substitutions in a particular regex literal to be performed just once, the first time it is evaluated. Otherwise, the substitutions will be performed every time the literal generates a Regexp object.

I could also turn up this usage example:

# avoid interpolating patterns like this if the pattern
# isn't going to change:
pattern = ARGV.shift
ARGF.each do |line|
    print line if line =~ /#{pattern}/
end

# the above creates a new regex each iteration. Instead,
# use the /o modifier so the regex is compiled only once

pattern = ARGV.shift
ARGF.each do |line|
    print line if line =~ /#{pattern}/o
end

So I guess this is rather a thing for the compiler, for a single line that is executed multiple times.

like image 62
Martin Ender Avatar answered Oct 20 '22 01:10

Martin Ender