Take the 2-minute tour ×

I have a text and want to find some strings with regular expressions. My problem is i don't know how to make "&&-and" here.

I have text like this:

AB " something is here;
  and here...
AB "new line 
 continues ... ";

I want to find all AB, which end with ";

My code i use for ";" :

var matches = Regex.Matches(tmp, "(AB) ([^;]*);", RegexOptions.Singleline);

But how can i make "(AB) ([^(\";)]*)\";" or just "(AB) ([^(\"&&;)]*)(\"&&;") ?

I would like to have:


" something is here;
 and here ...

"new line
 continues ..."
share|improve this question
And what exactly do you want to match? Could you perhaps show an example block of text, and then the part you want matched? –  Mr47 Nov 5 '12 at 10:25
add comment

3 Answers

up vote 2 down vote accepted

^ can only negate character classes, which in turn cannot contain strings of characters (but only single characters). However, there is a similar concept for strings of characters (or in fact full-fledged regular expressions) called a negative lookahead:

@"(AB) (\"(?:(?!\";).)*\");"

This will now consume arbitrary characters (.) as long as they do not mark the start of a ";. Keep using the Singleline option, of course. You can do some reading on lookaround assertions here.

share|improve this answer
please see my edit, what i want to achive. I tried your code but gives me some errors with @"AB ? –  Uni Le Nov 5 '12 at 11:21
@UniLe note that I my snippet is not a regex but the full regex-string (as a verbatim string, which should always be used for regular expressions). if you want to capture the two parts separately, you can just add parentheses: "(AB) (\"(?:(?!\";).)*\");". However, this is a bit pointless in your case, because the first two characters of your match will always be AB and the characters from index 3 to the second-to-last index, will be the rest. –  m.buettner Nov 5 '12 at 13:38
@m.buettned: thank you, your code works great. AB was just an example, it could be more of them like AB CD..., but what i wanted is your code, thanks once more –  Uni Le Nov 5 '12 at 15:01
when i have a string AB 134 aBd " ... "; how would i change your code to to it? something like "(AB) ([0-9]) ([A-Za-z0-9]) (\"(?:(?!\";).)*\");" –  Uni Le Nov 8 '12 at 9:09
@UniLe close, except for the fact that you need to repeat the character classes (otherwise they will just match a single character of the allowed ones): @"(AB) ([0-9]+) ([A-Za-z0-9]+) (\"(?:(?!\";).)*\");". Of course you can make all of the optional, so it still works with your initial examples: ``@"(AB)\s*([0-9]*)\s*([A-Za-z0-9]*)\s*(\"(?:(?!\";).)*\");"` –  m.buettner Nov 8 '12 at 9:12
show 8 more comments

I don’t think that AND operator exists or is even required. read this question

However to complete your string finding solution, this regex guide can help you a lot. Hope it helps.


share|improve this answer
add comment

Try this Regex:



(?= subexpression) Zero-width positive lookahead assertion.

(?: subexpression) Defines a noncapturing group.

. Wildcard: Matches any single character except \n.

\s Matches any white-space character.

share|improve this answer
what does my compiler dont' recognize \s? –  Uni Le Nov 5 '12 at 11:22
add comment

Your Answer


