1

Match a string that ends with a ; make it optional and do not capture that group as it is not required. But do capture the rest of the string that occurs after the first condition.

Text1: transfer from source not possible; snapmirror may be misconfigured, the source volume may be busy or unavailable.

Text2: snapmirror may be modified, the destination volume is unavailable.

Desired OUTPUT:

snapmirror may be misconfigured, the source volume may be busy or unavailable

snapmirror may be modified, the destination volume is unavailable

I want my regex to look for the 'transfer from source not possible' or any string that occurs in that way before a semi-colon and I want my regex not to capture this as a group.

Also, I want to capture everything that occurs after a semi-colon till the end.

Regex tried: (?:.*;)? (.+)\..*

The above regex works for Text1 but not for Text2. Anyone help me fix this please?

4

1 回答 1

0

在我看来,您的正则表达式应该可以工作,但它可能可以改进。

  • 匹配是否应该始终从字符串的开头开始?(这是一个棘手的问题;如果你不能做出这样的假设,那么这个问题就毫无意义。)

  • 是否会有多个分号?如果是这样,您是否希望非捕获部分仅扩展到第一个或最后一个?

  • 它应该总是在字符串的末尾结束,还是只想匹配句点?可以有多个时期吗?

这个最小更改的正则表达式似乎可以满足您的需求,并且添加的锚点可能会提高性能足以满足您的需求:

^(?:.*?;\s*)?(.+)\..*$

这个应该快得多;用.*否定字符类([^;]*[^.]+)替换几乎完全消除了回溯:

^(?:[^;]*;\s*)?([^.]+)

如果您使用支持它们的正则表达式风格,原子组和所有格量词可以使其更快:

^(?>[^;]*+;\s*+)?+([^.]++)
于 2013-06-11T08:16:38.203 回答