0

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

4
  • Did you want to match all the characters present inside the double quotes? Provide an actual string along with expected output. Commented Aug 2, 2015 at 11:20
  • In character sets you don't need to use the escape character. [A-z0-9\+;"]+ works the same. This regex would match all letters and numbers, as well as the back slash, + , ; and ". Is that what you're wanting? Commented Aug 2, 2015 at 11:23
  • @Flipybitz, Not actually. I haven't escaped those characters, I have escaped this character '\'. Commented Aug 2, 2015 at 11:27
  • Ah, I see what you were trying to accomplish, my bad I was wrong. Commented Aug 2, 2015 at 11:32

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Your solution covers much more than mine does, removed my post. I'd upvote yours if I hadn't reached my vote limit :)
Cooool, It's fantastic, but can you explain about it a little more or introduce me some link to read about it? (read about regular expression you have used :) I haven't seen something like this before)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.