正则表达式用于匹配文字字符串 [英] Regex for matching literal strings

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

问题描述

我试图写一个匹配字符串的正则表达式。为简单起见,我只关注双引号()字符串。



到目前为止,我有这个:\ [^ \] * \



这适用于大多数字符串,但在遇到像这样的转义双引号时失败:

 a string \带有转义引号

在这种情况下,它只匹配转义引用。



我尝试了几件事允许一个逃脱的报价,但到目前为止,我一直没有成功,任何人都可以帮我一把吗? 自己解决:

 \(\\。| [^ \\\]) * \


I'm trying to write a regular expression which will match a string. For simplicity, I'm only concerned with double quote (") strings for the moment.

So far I have this: "\"[^\"]*\""

This works for most strings but fails when there is an escaped double quote such as this:

"a string \" with an escaped quote"

In this case, it only matches up to the escaped quote.

I've tried several things to allow an escaped quote but so far I've been unsuccessful, can anyone give me a hand?

解决方案

I've managed to solve it myself:

"\"(\\.|[^\"\\])*\""

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

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