Various Regexp Options

Ruby regex literal can take parameters i, m, xwhich documented. But besides them, it can take a much wider range of options. Here is a list of options:

//e # => //
//i # => //i  ignore case
//m # => //m  multiline
//n # => //n
//o # => //
//s # => //
//u # => //
//x # => //x  extended
  • What are they doing? Are some of them related to coding? What about others?
  • If they specify an encoding, then what happens when more than one encoding is specified?
  • While other parameters cause an unknown regular expression parameter error, those listed here are missing. If the answer to the previous question is that they are not doing anything, then why are these specific options allowed?
  • Why is nreflected in the control, while others are not? Those whose inspection shows no differences are actually different?

, .

+4
2

:

. , :

Modifier    Description
i           Ignore case when matching text.
o           Perform #{} interpolations only once, the first time the regexp literal is evaluated.
x           Ignores whitespace and allows comments in regular expressions
m           Matches multiple lines, recognizing newlines as normal characters
u,e,s,n     Interpret the regexp as Unicode (UTF-8), EUC, SJIS, or ASCII. 
            If none of these modifiers is specified, the regular expression is 
            assumed to use the source encoding.

: . . .

+4

guido.

  • , , ( UTF-8 Ruby 2.0, ) , regex , US-ASCII 1.

  • , .

    //eu.encoding # => UTF-8
    //ue.encoding # => EUC
    
+2

Source: https://habr.com/ru/post/1538196/


All Articles