[英]Regex for escaping some single quotes
I was creating some regex for matching strings like : 'pulkit' '989' basically anything in between the two single quotes.我正在创建一些用于匹配字符串的正则表达式,例如: 'pulkit' '989' 基本上是两个单引号之间的任何内容。 so I created a regex something like ['][^']*[']
.所以我创建了一个类似于['][^']*[']
的正则表达式。
But this is not working for cases like: 'burger king's'.但这不适用于诸如“汉堡王”之类的情况。 The expected output is burger king's
but from my logic it is burger king
only.预期的输出是burger king's
但从我的逻辑来看,它只是burger king
。 As an another example 'pulkit'sharma'
the expected output should be pulkit'sharma
作为另一个例子'pulkit'sharma'
预期的输出应该是pulkit'sharma
So can anyone help me in this ?那么任何人都可以帮助我吗? How to escape single quotes in this case.在这种情况下如何转义单引号。
You may match single quote that is not preceded with a word char and is followed with a word char, and match any text up to the '
that is preceded with a word char and not followed with a word char:您可以匹配前面没有单词 char 而后跟单词 char 的单引号,并匹配任何文本直到'
前面有一个单词 char 而没有跟在单词 char 之后:
(?s)\B'\b(.*?)\b'\B
See the .NET regex demo .请参阅.NET 正则表达式演示。
Note you do not have to wrap single quotation marks with square brackets, they are not special regex metacharacters.请注意,您不必用方括号将单引号括起来,它们不是特殊的正则表达式元字符。
C# code: C#代码:
var matches = Regex.Matches(text, @"(?s)\B'\b(.*?)\b'\B")
.Cast<Match>()
.Select(x => x.Groups[1].Value)
.ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.