使用 Ruby 正则表达式匹配字符串数字
Match string numbers with Ruby regex
我需要帮助来将字符串与 ruby 正则表达式匹配。 (这是给木偶的)
- 我怎样才能匹配所有包含数字 001 到 010 的所有内容。
示例:master001、master002、master003
- 然后我需要匹配以 011 到 999 开头的所有内容。
例子:master011, master012 ..... master997, master998, master999
How can I match everything that has the numbers: 001 to 010, in the end.
\w+0(?:0[1-9]|10)
And then I need to match everything that starts with: 011 to 999 in the end.
\w+(?:0[1-9]|[1-9]\d)\d
我的第一次尝试是那些 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"
编辑:我的第二次尝试是那些:
"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"
第二个正则表达式捕捉到 010,但如果您已经在第一个正则表达式中咳嗽了,那也没关系。
无论如何感谢@Cyrbil。
"Everything" "How can I match everything..."。很模糊。 "everything" 可以包含任何字符,包括空格吗?完全由单词字符 ("cat_1001" =~ /\w+/ #=> 0
) 组成的 "cat_1001" 呢?该字符串以数字“1001”(的字符串表示)结尾,但最后三个字符是“001”?它应该是一场比赛吗?您是否要匹配字符串“007”(三位数字之前没有任何内容)?我假设您想匹配以下字符串:
- 从字符串的开头开始或前面有一个非字母
- 多一个字母(大写或小写)
- 有三位数
- 位于字符串末尾或后跟一个非数字
假设字符串是:
str = "Ann010, Bee012, Bob001 and Hank999a are MI6; 007, Deb0001 and Paul000 aren't"
应用我采用的匹配规则,第一组 (1-10) 由 Ann 和 Bob 组成;第二组(11-999),Bee和Hank。
这可以通过以下正则表达式完成:
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
提取候选人:
arr = str.scan(r)
#=> ["Ann010", "Bee012", "Bob001", "Hank999", "Deb000", "Paul000"]
然后可以根据需要提取:
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 的回答看起来不错,但它是一个思想家,它忽略了一些东西。你可以安全地使用稍微丑一点的:
/\w+(?:#{('001'..'010').to_a.join('|')})\b/
和
/\w+(?:#{('011'..'999').to_a.join('|')})\b/
我需要帮助来将字符串与 ruby 正则表达式匹配。 (这是给木偶的)
- 我怎样才能匹配所有包含数字 001 到 010 的所有内容。
示例:master001、master002、master003
- 然后我需要匹配以 011 到 999 开头的所有内容。
例子:master011, master012 ..... master997, master998, master999
How can I match everything that has the numbers: 001 to 010, in the end.
\w+0(?:0[1-9]|10)
And then I need to match everything that starts with: 011 to 999 in the end.
\w+(?:0[1-9]|[1-9]\d)\d
我的第一次尝试是那些 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"
编辑:我的第二次尝试是那些:
"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"
第二个正则表达式捕捉到 010,但如果您已经在第一个正则表达式中咳嗽了,那也没关系。
无论如何感谢@Cyrbil。
"Everything" "How can I match everything..."。很模糊。 "everything" 可以包含任何字符,包括空格吗?完全由单词字符 ("cat_1001" =~ /\w+/ #=> 0
) 组成的 "cat_1001" 呢?该字符串以数字“1001”(的字符串表示)结尾,但最后三个字符是“001”?它应该是一场比赛吗?您是否要匹配字符串“007”(三位数字之前没有任何内容)?我假设您想匹配以下字符串:
- 从字符串的开头开始或前面有一个非字母
- 多一个字母(大写或小写)
- 有三位数
- 位于字符串末尾或后跟一个非数字
假设字符串是:
str = "Ann010, Bee012, Bob001 and Hank999a are MI6; 007, Deb0001 and Paul000 aren't"
应用我采用的匹配规则,第一组 (1-10) 由 Ann 和 Bob 组成;第二组(11-999),Bee和Hank。
这可以通过以下正则表达式完成:
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
提取候选人:
arr = str.scan(r)
#=> ["Ann010", "Bee012", "Bob001", "Hank999", "Deb000", "Paul000"]
然后可以根据需要提取:
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 的回答看起来不错,但它是一个思想家,它忽略了一些东西。你可以安全地使用稍微丑一点的:
/\w+(?:#{('001'..'010').to_a.join('|')})\b/
和
/\w+(?:#{('011'..'999').to_a.join('|')})\b/