2

After doing a fair amount of research I landed on the SO community wiki where it was said that the best way to see if something doesn't match in regex is to do something like this:

^((?!hede).)*$

So using that as an example, I tried to get a match for anything in parenthesis that doesn't start with the letters fe or Fe. I came up with this:

\(((?!fe|Fe).+?)\)

It works, but it seems to be giving me two matches, one containing the parenthesis and one without them. Can someone please point out what im doing wrong.

Community
  • 1
  • 1

2 Answers2

4

It does not give you two matches. It just gives you the full match an the first captured group. Simply remove the outer pair of unescaped parentheses and the second "match" should go away:

\((?!fe|Fe).+?\)

If you need those parentheses later on, but still don't need that captured substring it would generate, you can also make parentheses non-capturing with ?::

\((?:(?!fe|Fe).+?)\)

These two are equivalent, but the latter is slightly redundant.

Also, it is generally considered good practice to use negative character classes instead of ungreedy repetition, if you want to match some delimited content:

\((?!fe|Fe)[^)]+\)

(This should be equivalent)

Martin Ender
  • 43,427
  • 11
  • 90
  • 130
  • @Bahamut since the OP is using .NET, I'd rather recommend http://regexhero.net/tester/ – Martin Ender Oct 28 '12 at 17:06
  • @m.buettner .NET's regex is similar to that being used in regexpal. silverlight, as I remember, uses a regex pattern different from plain .NET. – Bahamut Oct 28 '12 at 17:26
  • +1 You have certainly taken on these regex questions with gusto and a vengeance. My compliments! Have you read [Mastering Regular Expressions (3rd Edition)](http://www.amazon.com/Mastering-Regular-Expressions-Jeffrey-Friedl/dp/0596528124 "By Jeffrey Friedl. Best book on Regex - ever!") yet? If you haven't, I can guarantee that you will thoroughly enjoy it. It is hands down the most useful book I've ever read. And once again, nice job. – ridgerunner Oct 28 '12 at 19:28
  • @ridgerunner thanks a lot for the recommendation, I will certainly check it out! In fact I was eschewing regexes for a quite a long time - not because of the theoretical bits, but because of the slightly daunting syntax. When I registered at SO I finally took this question tag as a reason to get my head around it... and in the end it turns out to be pretty simple, and *almost* the same in all languages. So, I'm pretty glad I finally got around to learn them, and really enjoy the challenging of solving all these regex questions here ;) – Martin Ender Oct 28 '12 at 19:34
  • @Bahamut, hm okay, I didn't know about that. But the differences between JS and .NET in terms of the regex flavor are also not really negligible (Unicode and lookbehinds come to my mind right now) – Martin Ender Oct 28 '12 at 19:36
  • 1
    @Bahamut - Silverlight's Regex implementation is identical to that of .NET, except that it's missing the RegexOptions.Compiled enumeration. I include a little blurb about this on the homepage of http://regexhero.net/ under "Is Regex Hero completely compatible with the .NET flavor of regular expressions?" – Steve Wortham Oct 28 '12 at 21:13
  • @m.buettner - You're very lucky. I had been programming for more than 30 years before I discovered (and then fell in love with) regex 5 years ago. I kick myself a thousand times for not learning them earlier. Mastering them is well worth the effort! P.S. Since you're into regex, you might like to check out my [dynamic regex highlighter](http://jmrware.com/articles/2010/dynregexhl/DynamicRegexHighlighterTester.html) script. (The script uses non-recursive (js) regex to iteratively parse the nested bracket structures of PCRE regex syntax.) – ridgerunner Oct 29 '12 at 01:27
  • @SteveWortham I stand corrected then. Thanks for clearing up. – Bahamut Oct 29 '12 at 04:29
  • @Bahamut - Sure. By the way, this is also documented by Microsoft. They say, "The implementation of the regular expression engine in the .NET Framework for Silverlight is identical to that in the .NET Framework. The single exception is that the .NET Framework for Silverlight does not support compiled regular expressions, which are predefined regular expression patterns that are stored in stand-alone assemblies together with dedicated regular expression engines that process text using those regular expression patterns." http://msdn.microsoft.com/en-us/library/hs600312(v=vs.95).aspx – Steve Wortham Oct 29 '12 at 17:26
1

You can remove capture group by adding "?:" to the first bracket.

Skpd
  • 670
  • 3
  • 17