Saeed mohammadi
Saeed mohammadi

Reputation: 319

C# string pattern in regex

What's the pattern of C# strings in regular expression? I mean some pattern that matches anything that Visual Studio recognizes as string

This pattern is what I have tried but it doesn't work in all cases :

"[A-z0-9\\+;"]+"

(of course this is not complete and there are a lot more characters that should be included but even here it's not working properly)

It works for a text like this : "kla\"+;s" but in this case : "a\"b + c" it only matches to "a\".

Actually I have testes a lot more but none of them were successful.

First of all I thought about this pattern: ".*" which won't work properly in a scenario like this : "a\"+;b" + "a\"b + c" which is not a string in fact but two separate strings and a plus operator

Upvotes: 0

Views: 161

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174874

To match all the double quoted blocks.

@"(?<!\\)"".*?(?<!\\)"""

DEMO

Explanation:

  • (?<!\\)" negative lookbehind which asserts that the match " must not be preceded by a backslash. In C# double "" means a single double quotes. Another " is used just for escaping purpose. So this ensures that the starting double quotes must not be an escaped one.

  • .*? Non-greedy pattern which matches all the characters non-greedily until

  • (?<!\\)" an unescaped double quotes was found.

Upvotes: 3

Related Questions