[英]Match string numbers with Ruby regex
I need help to match string(s) with ruby regular expression. 我需要帮助将字符串与ruby正则表达式匹配。 (it's for puppet)
(是给木偶的)
Example: master001, master002, master003 示例:master001,master002,master003
Example: master011, master012 ..... master997, master998, master999 示例:master011,master012 ..... master997,master998,master999
How can I match everything that has the numbers: 001 to 010, in the end.
最后,我该如何匹配所有数字:001到010。
\w+0(?:0[1-9]|10)
And then I need to match everything that starts with: 011 to 999 in the end.
然后,我需要匹配所有以011到999开头的内容。
\w+(?:0[1-9]|[1-9]\d)\d
See it live here and here 在这里和这里看到它
And as suggested by @Cary, you can run it with str.scan
就像@Cary所建议的那样,您可以使用
str.scan
运行它
My first attempt would be those 2 : 我的第一次尝试将是那些2:
"master001".match(/010$|00[1-9]$/) #=> "001" up to "009" "010"
"master099".match(/0[1-9]\d$|[1-9]\d\d$/) #=> "011" up to "999"
Edit : My 2nd attempt would be those : 编辑 :我的第二次尝试将是那些:
"master001".match(/010$|00[1-9]$/) #=> "001" up to "009" "010"
"master099".match(/0[1-9]\d$|[1-9]\d\d$/) #=> "010" up to "999"
The second regex catches 010 but that's okay if you already cought it in the 1st one. 第二个正则表达式可以捕获010,但是如果您已经在第一个正则表达式中使用了它就可以了。
Anyway kudos to @Cyrbil. 无论如何@Cyrbil。
"Everything" in "How can I match everything...". “如何匹配所有内容...”中的“所有内容”。 is quite vague.
相当模糊。 Can "everything" contain any characters, including spaces?
“所有内容”都可以包含任何字符,包括空格吗? What about "cat_1001", which is comprised entirely of word characters (
"cat_1001" =~ /\\w+/ #=> 0
)? 那么完全由文字字符组成的
"cat_1001" =~ /\\w+/ #=> 0
如何呢?( "cat_1001" =~ /\\w+/ #=> 0
)? That string ends with the (string representation of the) number "1001" but whose last three characters are "001"? 该字符串以数字“ 1001”(的字符串表示形式)结尾,但其后三个字符为“ 001”? Should it be a match?
应该是火柴吗? Do you want to match the string "007" (three digits with nothing before)?
您是否要匹配字符串“ 007”(三位数字,前没有数字)? I have assumed you want to match strings that:
我假设您要匹配以下字符串:
Suppose the string were: 假设字符串为:
str = "Ann010, Bee012, Bob001 and Hank999a are MI6; 007, Deb0001 and Paul000 aren't"
Applying the rules for matching that I've adopted, the first group (1-10) is comprised of Ann and Bob; 应用我采用的匹配规则,第一组(1-10)由Ann和Bob组成; the second group (11-999), Bee and Hank.
第二组(11-999),蜜蜂和汉克。
This can be accomplished with the following regex: 可以使用以下正则表达式完成此操作:
r = /
[a-z]+ # match one or more letters
\d{3} # match three digits
# (?!\d) # do not match another digit (negative lookahead)
/ix # case-indifferent and extended/free-spacing modes
to extract candidates: 提取候选人:
arr = str.scan(r)
#=> ["Ann010", "Bee012", "Bob001", "Hank999", "Deb000", "Paul000"]
which can then be extracted as desired: 然后可以根据需要提取:
arr.select { |s| (1..10).cover? s[-3..-1].to_i }
#=> ["Ann010", "Bob001"]
arr.select { |s| (11..999).cover? s[-3..-1].to_i }
#=> ["Bee012", "Hank999"]
Cyrbil's answer looks nice but it's a thinker and it overlooks stuff. Cyrbil的回答看起来不错,但它是一个思想家,却忽略了某些东西。 You can play it safe with the somewhat uglier:
您可以使用较为丑陋的方法来确保安全:
/\w+(?:#{('001'..'010').to_a.join('|')})\b/
and 和
/\w+(?:#{('011'..'999').to_a.join('|')})\b/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.