2

I'm using regex in powershell 5.1.

I need it to detect groups of numbers, but ignore groups followed or preceeded by /, so from this it should detect only 9876.

[regex]::matches('9876 1234/56',‘(?<!/)([0-9]{1,}(?!(\/[0-9])))’).value

As it is now, the result is:

9876

123

6

More examples: "13 17 10/20" should only match 13 and 17.

Tried using something like (?!(\/([0-9]{1,}))), but it did not help.

4

1 回答 1

3

您可以使用

\b(?<!/)[0-9]+\b(?!/[0-9])

查看正则表达式演示

或者,如果数字可以粘贴到文本上:

(?<![/0-9])[0-9]+(?!/?[0-9])

请参阅此正则表达式演示

第一个模式基于单词边界\b,确保_在预期匹配之前和之后没有字母、数字和正确。第二个只是确保没有数字并且/在比赛的两端。

细节

  • (?<![/0-9])- 一个消极的向后看,确保没有数字或/紧邻当前位置的左侧
  • [0-9]+- 一个或多个数字
  • (?!/?[0-9])- 一个负前瞻,确保没有可选项/,紧跟当前位置右侧的数字。
于 2018-12-05T09:05:15.263 回答