0

我正在尝试匹配这些行的开头以获取数字

1 - blah
01 - blah

我预计

1
01

我有这个正则表达式,但不明白为什么第二部分不匹配 01

((^\d)|(^\d\d))

谢谢你

4

1 回答 1

1

您的模式与^.

  • ^Mode modifier除非您使用或 other ,否则将匹配字符串的开头options

试试这个

(?im)^(\d+)\b

解释

<!--
(?im)^(\d+)\b

Match the remainder of the regex with the options: case insensitive (i); ^ and $ match at line breaks (m) «(?im)»
Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
Match the regular expression below and capture its match into backreference number 1 «(\d+)»
   Match a single digit 0..9 «\d+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Assert position at a word boundary «\b»
-->
于 2012-05-26T06:31:13.833 回答