Tuesday 2 January 2018

ruby - Inspection of 'Regexp.union'





Regexp.union returns the following
inspection:




Regexp.union(/dogs/,
/cats/i) #=>
/(?-mix:dogs)|(?i-mx:cats)/


What
does ?-mix: mean?


class="post-text" itemprop="text">
class="normal">Answer



What
you're seeing is a representation of options on sub-regexes. The options to the left of
the hyphen are on, and the options to the right of the hyphen are
off. It's smart to explicitly set each option as on or off to
ensure the right behavior if this regex ever became part of a larger
one.



In your example,
(?-mix:dogs) means that the m,
i, and x options are all off whereas
in (?i-mx:cats), the i option is on
and thus that subexpression is
case-insensitive.



See the Ruby docs on href="https://ruby-doc.org/core-2.4.2/Regexp.html#class-Regexp-label-Options"
rel="nofollow noreferrer">Regexp
Options:






The end delimiter for a regexp can be followed by one or more single-letter
options which control how the pattern can match.





  • /pat/i - Ignore
    case

  • /pat/m - Treat a newline
    as a character matched by .


  • /pat/x - Ignore whitespace and comments in the
    pattern

  • /pat/o - Perform #{}
    interpolation only once





i, m, and x can also be applied on the subexpression level with the
(?on-off) construct, which enables options
on, and disables options off for the
expression enclosed by the
parentheses.




No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...