Reputation: 331
I want to match a particular word which is followed by double quotes. I am using regex @"\bspecific\S*id\b" which will match anything that starts with specific and ends with id. But, I want something which should match
"specific-anything-id"
(it should be with double quotes)
**<specific-anything-id>**
- should not match
specific-"anything"-id
- should not match
Upvotes: 1
Views: 157
Reputation: 626748
You can include the double quotes and use a negated character class [^"]
(matching any char but "
) rather than \S
(that can also match double quotes as it matches any non-whitespace character):
var pattern = @"""specific[^""]*id""";
You do not need word boundaries either here.
See the regex demo and a C# demo:
var s = "\"specific-anything-id\" <specific-anything-id> specific-\"anything\"-id";
var matches = Regex.Matches(s, @"""specific[^""]*id""");
foreach (Match m in matches)
Console.WriteLine(m.Value); // => "specific-anything-id"
Upvotes: 1