What is '?-mix' in a Ruby Regular Expression

后端 未结 2 1650
花落未央
花落未央 2020-12-09 02:03

Just trying to debug a regular expression in ruby. When I print the contents of a regular expression, it shows ?-mix at the beginning of the regular expression

相关标签:
2条回答
  • 2020-12-09 02:30

    mix is not the English word mix, it's options of Regexp.

    See Regexp#to_s:

    Returns a string containing the regular expression and its options (using the (?opts:source) notation.

    In your example, m is for multiline mode, i is for case insensitive, and x is for extended mode. Options before the dash are on, those after are off (default). The question's example, ?-mix, has all options off.

    You can turn them on like:

    puts /^a$/mix
    # =>(?mix:^a$)
    
    0 讨论(0)
  • 2020-12-09 02:52

    Regarding the - it's a syntax for flags. Those before the dash are on, and those after are off.

    As expalined in the Regexp docs, this is an inline modifier, using the (?on-off) syntax:

    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.

    Hence, in my case this means the options m, i, and x are off and none are on.

    0 讨论(0)
提交回复
热议问题