如何使用常规的前pression检测无效Ç转义字符串? [英] How to detect an invalid C escaped string using a regular expression?

查看:164
本文介绍了如何使用常规的前pression检测无效Ç转义字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找个正规的前pression(正则表达式),如果你有一个C双引号转义字符串(在这里你可以找到双引号才躲过了)一些无效的转义,它探测到。

我认为有效的 \\\\ \\ n \\ r \\(测试字符串使用)

一个部分解决方案是使用(小于?!\\\\)\\\\ [^ \\\\\\ NR] 但是这一次没有检测到不良逃逸像 \\\\\\

下面是我用来测试匹配的测试字符串:

... \\ n \\\\ ... ... b \\... ... \\\\\\\\ \\\\\\ E. .. \\ ... \\\\\\。 .. \\\\\\\\\\ ...... \\ E ...

这位前pression应该匹配在过去的6块为无效,第4是有效的。问题是,我目前的版本并发现只有2/5的错误。


解决方案

 (?:^ | [^ \\\\])(?:\\\\\\\\)*((?: \\| \\\\(?:[^ \\\\\\ NR] | $)))

这是一个字符串或东西是不是一个反斜杠的开始。然后一些(可能是零)正确地转义反斜线,那么要么是转义或其他反斜线;如果它是另一个反斜杠,它必须遵循的东西,既不是 \\ N ,也不研究或字符串的结尾。

不正确的逃生被捕获你。

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.

解决方案

(?:^|[^\\])(?:\\\\)*((?:\"|\\(?:[^\"\\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.

这篇关于如何使用常规的前pression检测无效Ç转义字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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