匹配转义字符的正则表达式(引号) [英] Regular expression to match escaped characters (quotes)

查看:470
本文介绍了匹配转义字符的正则表达式(引号)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想构建一个简单的正则表达式,其中包含引用的字符串,包括其中的任何转义的引号。例如,

I want to build a simple regex that covers quoted strings, including any escaped quotes within them. For instance,

"This is valid"
"This is \" also \" valid"

显然,像

"([^"]*)"

不起作用,因为它符合第一个转义的报价。

does not work, because it matches up to the first escaped quote.

正确的版本是什么?

我想对于其他转义的字符(通过替换相应的字符),答案将是一样的。

I suppose the answer would be the same for other escaped characters (by just replacing the respective character).

顺便说一句,意识到全部正则表达式

By the way, I am aware of the "catch-all" regex

"(.*?)"

但是我尽可能避免它,因为毫不奇怪,它运行稍微比一个更具体的一个。

but I try to avoid it whenever possible, because, not surprisingly, it runs somewhat slower than a more specific one.

推荐答案

所有其他答案的问题是它们只匹配初始的明显测试,但不足以进一步研究例如,所有答案都希望第一个引用不会被转义。但最重要的是,转义是一个更复杂的过程,而不仅仅是一个反斜杠,因为反斜杠本身可以被转义。想象一下,尝试实际匹配一个以反斜杠结尾的字符串。这可能如何?

The problem with all the other answers is they only match for the initial obvious testing, but fall short to further scrutiny. For example, all of the answers expect that the very first quote will not be escaped. But most importantly, escaping is a more complex process than just a single backslash, because that backslash itself can be escaped. Imagine trying to actually match a string which ends with a backslash. How would that be possible?

这将是您要查找的模式。它不认为第一个引用是工作的,它将允许反斜杠被转义。

This would be the pattern you are looking for. It doesn't assume that the first quote is the working one, and it will allow for backslashes to be escaped.

(?<!\\)(?:\\{2})*"(?:(?<!\\)(?:\\{2})*\\"|[^"])+(?<!\\)(?:\\{2})*"

这篇关于匹配转义字符的正则表达式(引号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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