Reputation: 327
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
Upvotes: 0
Views: 359
Reputation: 22837
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). You can use one of the following methods:
(?<!['"])\bstring\b(?!['"])
- see it in use here
(?<!['"])
- ensure "
doesn't precede.(?!['"])
- ensure "
doesn't proceeds.(?<=\s|^)\bstring\b(?=\s|$)
- see it in use here
(?<=\s|^)
- ensure whitespace or the beginning of the line precedes.(?=\s|$)
- ensure whitespace or the end of the line proceeds.(?<=\s|^)string(?!\s+(?!stop)|$)
would ensure the word isn't followed by the word 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.(?<=\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).Upvotes: 2
Reputation: 40517
This regex will pick string if it is between spaces: \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}");
}
Upvotes: 1