如何使用常规的前pression检测无效Ç转义字符串?

编程入门 行业动态 更新时间:2024-10-28 14:25:21
本文介绍了如何使用常规的前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检测无效Ç转义字符串?

本文发布于:2023-06-10 16:31:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/615069.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   如何使用   常规   pression

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!