修复正则表达式中的灾难性回溯 [英] Fixing Catastrophic Backtracking in Regular Expression

查看:165
本文介绍了修复正则表达式中的灾难性回溯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下正则表达式来检查有效的文件路径:

I'm using the following regular expression to check for valid file paths:

^(?:[a-zA-Z]\:\\|\\\\)([^\\\/\:\*\?\<\>\"\|]+(\\){0,1})+$

使用测试字符串V:\Sample Names\Libraries\DeveloperLib\DeveloperComDlgs\res被识别为有效.我什至可以将无效字符添加到字符串的开头,而不会出现问题.但是,当我在字符串的末尾添加无效字符时,该网页就会从灾难性的回溯中冻结.

Using the test string V:\Sample Names\Libraries\DeveloperLib\DeveloperComDlgs\res is recognized as valid. I can even add invalid characters to the beginning of the string without issues. However, when I add an invalid character towards the end of the string, the webpage freezes up from catastrophic backtracking.

此正则表达式字符串中的原因是什么?

What is causing this in this regex string?

完整字符串: ^(?:[a-zA-Z]\:\\|\\\\)([^\\\/\:\*\?\<\>\"\|]+(\\){0,1})+$

第一组: (?:[a-zA-Z]\:\\|\\\\)

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆