简体   繁体   English

正则表达式性能问题

[英]Regex performance issues


I have to construct a regex for angularjs ng-pattern attribute.我必须为 angularjs ng-pattern 属性构建一个正则表达式。 The regex has to validate a text, not each line or some pieces.正则表达式必须验证文本,而不是每一行或某些片段。 This is the format i have to validate: some text with spaces and punctuation:digits.2digits这是我必须验证的格式:一些带有空格和标点符号的文本:digits.2digits

For example this text is valid:例如,此文本有效:

name:1234.32 name:234.43<br>
name:43.22

For example this text is not valid because one group starts with ":":例如,此文本无效,因为一组以“:”开头:

name:1234.32 :234.43<br>
name:43.22

For example this text is not valid because last group doesn't end with 2 decimals:例如,此文本无效,因为最后一组不以 2 位小数结尾:

name:1234.32 name:234.43 name:43.2

I have build some regex but in case i have a bigger text, it fails with timeout .我已经构建了一些正则表达式,但如果我有更大的文本,它会因timeout失败。 It fails only when last group is invalid.只有当最后一组无效时它才会失败。 Here is an example.是一个例子。
This is my regex:这是我的正则表达式:

^(([\S\s]+)\s*:\s*([0-9]+[.][0-9]{2})\s*)+$

Can you help me optimize this regex?你能帮我优化这个正则表达式吗?

Your own regex has multiple following quantifiers with no limitation on matching characters that causes engine to encounter a catastrophic backtracking almost on failure on large data.您自己的正则表达式有多个以下量词,对匹配字符没有限制,这会导致引擎几乎在大数据失败时遇到灾难性的回溯

You'd go with a regex like this:你会使用这样的正则表达式:

^(([^:]*)\S\s*:\s*\d+\.\d{2}(?!\d))*$

Live demo现场演示

Breakdown:分解:

^   # Assert beginning of input string
(   # Construct a group #1
    ([^:]*) # Construct group #2, match anything except `:`
    \S\s*:\s*\d+\.\d{2}(?!\d)   # Match a valid sequence
)*  # Repeat #1 as much as possible
$   # End of input string

Try this ^(([\\S\\s]+)\\s*?:\\s*?([0-9]+[.][0-9]{2})\\s*?)+$试试这个^(([\\S\\s]+)\\s*?:\\s*?([0-9]+[.][0-9]{2})\\s*?)+$

But I recommend if you can, split text on lines and evaluate each one, and stop at line that not complain the test.但我建议如果可以的话,将文本分行并评估每个文本,并在不抱怨测试的行处停止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM