[英]How to match words that doesn't start nor end with certain characters using Regex?
I want to find word matches that doesn't start nor end with some specific characters.我想找到不以某些特定字符开头或结尾的单词匹配。
For example, I have this input and I only want to match the highlighted word:例如,我有这个输入,我只想匹配突出显示的单词:
"string" string 'string'
“字符串”字符串“字符串”
And exclude other words that start and end with either " or ' .并排除其他以"或'开头和结尾的单词。
I am currently using this pattern:我目前正在使用这种模式:
But I do not know what pattern I should use that would exclude words that start and end with some specified characters.但是我不知道应该使用什么模式来排除以某些指定字符开头和结尾的单词。
Can some one give me some advice on what pattern I should use?有人可以就我应该使用什么模式给我一些建议吗? Thank you
谢谢
The pattern you're currently using matches since \b
properly asserts the positions between "s
and g"
(a position between a word character [a-zA-Z0-9_]
and a non-word character).您当前使用的模式匹配,因为
\b
正确断言了"s
和g"
之间的位置(单词字符[a-zA-Z0-9_]
和非单词字符之间的 position)。 You can use one of the following methods:您可以使用以下方法之一:
(?<?['"])\bstring\b(?!['"])
- see it in use here (?<?['"])\bstring\b(?!['"])
-在此处查看它的使用情况
(?<!['"])
- ensure "
doesn't precede. (?<!['"])
- 确保"
不在前面。(?!['"])
- ensure "
doesn't proceeds. (?!['"])
- 确保"
不会继续。(?<=\s|^)\bstring\b(?=\s|$)
- see it in use here (?<=\s|^)\bstring\b(?=\s|$)
-在此处查看它的使用情况
(?<=\s|^)
- ensure whitespace or the beginning of the line precedes. (?<=\s|^)
- 确保空格或行首在前。(?=\s|$)
- ensure whitespace or the end of the line proceeds. (?=\s|$)
- 确保空格或行尾继续。(?<=\s|^)string(??\s+(?!stop)|$)
would ensure the word isn't followed by the word stop
(?<=\s|^)string(??\s+(?!stop)|$)
之类的东西会确保单词后面没有单词stop
(?<=(?<?stop\s*)\s+|^)string(?=\s+|$)
would ensure the word doesn't follow the word stop
- note that quantifiers ( \s+
) in lookbehinds are not allowed in most regex engines, .NET allows it. (?<=(?<?stop\s*)\s+|^)string(?=\s+|$)
之类的东西会确保单词不跟随单词stop
- 请注意后视中的量词( \s+
)在大多数正则表达式引擎中是不允许的,.NET 允许它。(?<=\s|^)\bstring\b(?=\s|$)(?!\z)
would ensure a the word isn't at the end of the string (different from end of line if multi-line). (?<=\s|^)\bstring\b(?=\s|$)(?!\z)
之类的东西会确保单词不在字符串的末尾(如果多行)。 This regex will pick string if it is between spaces: \sstring\s
如果它在空格之间,此正则表达式将选择字符串:
\sstring\s
var sample = "\"string\" string \"string\" astring 'string_ string?string string ";
var regx = new Regex(@"\sstring\s");
var matches = regx.Matches(sample);
foreach (Match mt in matches)
{
Console.WriteLine($"{mt.Value} {mt.Index,3} {mt.Length,3}");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.