Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
827 views
in Technique[技术] by (71.8m points)

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

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 even though those characters were not part of the expression. Please see the following IRB output to see this illustrated

irb(main):028:0* EXPR = /^a$/
=> /^a$/
irb(main):029:0> EXPR
=> /^a$/
irb(main):030:0> puts EXPR
(?-mix:^a$)
=> nil

As you can see, when you use puts to print out the contents of a regular expression, there is ?-mix at the beginning. Should I be concerned by this? Where is it coming from?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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$)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...