如何用正则表达式匹配双引号或单引号或不带引号的? [英] How to match double quote or single quote or unquoted with regular expression?
本文介绍了如何用正则表达式匹配双引号或单引号或不带引号的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图从所有三种类型的输入中获取一些文本,但无法弄清楚如何处理未引用的情况.
I am trying to grab sometext from all three types of inputs, but can't figure out how to deal with the unquoted case.
到目前为止我有:
name=['"](.*?)['"]
输入:
name="sometext"
name='sometext'
name=sometext
推荐答案
看起来您是 C# 开发人员,因此您可以使用第一个匹配组来确保它以相同的引号关闭(从而支持 phrase="不要忘记撇号"
).
It looks like you are a C# developer, so you can use the first matching group to ensure it is closed off with the same quote (and thus support phrase="Don't forget apostrophes"
).
Regex regex1 = new Regex(@"=(?:(['""])(.*?)\1|.*)");
string text = @"
name=""don't forget me""
name='sometext'
name='sometext'
name=sometext
";
foreach (Match m in regex1.Matches(text))
Console.WriteLine (m.Groups[2].Value);
这篇关于如何用正则表达式匹配双引号或单引号或不带引号的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文