正则表达式只能查找数字但不会在字符串中进行测试

时间:2016-05-05 17:11:32

标签: regex

我有一个字符串为"测试我找到这个[测试$ 12345]并且没有找到[测试$ SS7890]这个"。我必须找到12345而不是7890.

1 个答案:

答案 0 :(得分:0)

描述

这个正则表达式将:

  • 匹配[test$]
  • 之间的数字

正则表达式:

(?<=\[test\$)([0-9]+(?=]))

Regular expression visualization

直播示例

https://regex101.com/r/bY1kH8/3

解释

(?<=\[test\$) Positive Lookbehind - Assert that the regex below can be matched
\[ matches the character [ literally
test matches the characters test literally (case sensitive)
\$ matches the character $ literally
1st Capturing group ([0-9]+(?=]))
[0-9]+ match a single character present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
0-9 a single character in the range between 0 and 9
(?=]) Positive Lookahead - Assert that the regex below can be matched
] matches the character ] literally