如何使用 Regexp.union 构建不区分大小写的正则表达式 [英] How to build a case-insensitive regular expression with Regexp.union
问题描述
我有一个字符串列表,需要从它们构建正则表达式,使用 Regexp#union
.我需要生成的模式不区分大小写.
I have a list of strings, and need to build the regular expression from them, using Regexp#union
. I need the resulting pattern to be case insensitive.
#union
方法本身不接受选项/修饰符,因此我目前看到两个选项:
The #union
method itself does not accept options/modifiers, hence I currently see two options:
strings = %w|one two three|
Regexp.new(Regexp.union(strings).to_s, true)
和/或:
Regexp.union(*strings.map { |s| /#{s}/i })
两种变体看起来都有些奇怪.
Both variants look a bit weird.
是否可以使用 Regexp.union
构建不区分大小写的正则表达式?
Is there an ability to construct a case-insensitive regular expression by using Regexp.union
?
推荐答案
简单的起点是:
words = %w[one two three]
/#{ Regexp.union(words).source }/i # => /one|two|three/i
您可能想确保您只匹配单词,因此将其调整为:
You probably want to make sure you're only matching words so tweak it to:
/\b#{ Regexp.union(words).source }\b/i # => /\bone|two|three\b/i
为了干净和清晰,我更喜欢使用非捕获组:
For cleanliness and clarity I prefer using a non-capturing group:
/\b(?:#{ Regexp.union(words).source })\b/i # => /\b(?:one|two|three)\b/i
使用source
很重要.当您创建一个 Regexp 对象时,它会了解应用于该对象的标志(i
、m
、x
),并且这些标志插入到字符串中:
Using source
is important. When you create a Regexp object, it has an idea of the flags (i
, m
, x
) that apply to that object and those get interpolated into the string:
"#{ /foo/i }" # => "(?i-mx:foo)"
"#{ /foo/ix }" # => "(?ix-m:foo)"
"#{ /foo/ixm }" # => "(?mix:foo)"
或
(/foo/i).to_s # => "(?i-mx:foo)"
(/foo/ix).to_s # => "(?ix-m:foo)"
(/foo/ixm).to_s # => "(?mix:foo)"
当生成的模式独立时很好,但是当它被插入到一个字符串中以定义模式的其他部分时,标志会影响每个子表达式:
That's fine when the generated pattern stands alone, but when it's being interpolated into a string to define other parts of the pattern the flags affect each sub-expression:
/\b(?:#{ Regexp.union(words) })\b/i # => /\b(?:(?-mix:one|two|three))\b/i
深入研究 Regexp 文档,您会看到 ?-mix
关闭了 (?-mix:one|two|three)
中的忽略大小写",即使整个模式被标记为 i
,导致模式不符合你的要求,并且很难调试:
Dig into the Regexp documentation and you'll see that ?-mix
turns off "ignore-case" inside (?-mix:one|two|three)
, even though the overall pattern is flagged with i
, resulting in a pattern that doesn't do what you want, and is really hard to debug:
'foo ONE bar'[/\b(?:#{ Regexp.union(words) })\b/i] # => nil
相反,source
删除了内部表达式的标志,使模式符合您的预期:
Instead, source
removes the inner expression's flags making the pattern do what you'd expect:
/\b(?:#{ Regexp.union(words).source })\b/i # => /\b(?:one|two|three)\b/i
和
'foo ONE bar'[/\b(?:#{ Regexp.union(words).source })\b/i] # => "ONE"
您可以使用 Regexp.new
并传入标志来构建您的模式:
You can build your patterns using Regexp.new
and passing in the flags:
regexp = Regexp.new('(?:one|two|three)', Regexp::EXTENDED | Regexp::IGNORECASE) # => /(?:one|two|three)/ix
但是随着表达式变得越来越复杂,它变得笨拙.使用字符串插值构建模式仍然更容易理解.
but as the expression becomes more complex it becomes unwieldy. Building a pattern using string interpolation remains more easy to understand.
这篇关于如何使用 Regexp.union 构建不区分大小写的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!