How to detect an invalid C escaped string using a regular expression?

独自空忆成欢 提交于 2019-12-08 12:02:10

问题


I would like to find a regular expression (regex) that does detect if you have some invalid escapes in a C double quoted escaped string (where you can find double quotes only escaped).

I consider valid \\ \n \r \" (the test string is using ")

A partial solution to this is to use (?<!\\)\\[^\"\\nr] but this one fails to detect bad escapes like \\\.

Here is a test string that I use to test the matching:

...\n...\\b...\"...\\\\...\\\E...\...\\\...\\\\\..."...\E...

The expression should match the last 6 blocks as invalid, the first 4 are valid. The problem is that my current version does find only 2/5 errors.


回答1:


(?:^|[^\\])(?:\\\\)*((?:\"|\\(?:[^\"\\nr]|$)))

That's the start of a string, or something that's not a backslash. Then some (possibly zero) properly escaped backslashes, then either an unescaped " or another backslash; if it's another backslash, it must be followed by something that is neither ", \, n, nor r, or the end of the string.

The incorrect escape is captured for you as well.




回答2:


Try this regular expression:

^(?:[^\\]+|\\[\\rn"])*(\\(?:[^\\rn"]|$))

If you have a match, you have an invalid escape sequence.



来源:https://stackoverflow.com/questions/1873652/how-to-detect-an-invalid-c-escaped-string-using-a-regular-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!