user6639252
user6639252

Reputation: 331

Match a particular word after double quotes - c#,regex

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

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

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

heemayl
heemayl

Reputation: 41997

Do:

"([^"]+)"

the matched group would contain the ID you want.

Upvotes: 0

Related Questions