zcalebz zcalebz - 5 months ago 32
Perl Question

Match lines not starting with first two specific characters

I'm trying to match lines that do not begin with "CD" or any 2-digit number. I've tried:

^[^Cc0-9][^Dd0-9].+?$


but it does not match lines that begin with "Cx" or "0y".

I'm using a program called rxrepl with the above regex as the search string and nothing as the replacement. I'm trying to avoid using grep. Also I can't use parentheses because in rxrepl they are used to capture groups.

Answer

As others have said, you need

^(?:CD|\d\d)

The (?: ... ) is a non-capturing group

Or if you must avoid parentheses of any sort then use

^CD|^\d\d



Update

To comply with your new spec "I'm actually trying to match lines not beginning with CD or 2 digit numbers"

^(?!CD|\d\d)

You need to get over your problem with the parentheses. A negative look-ahead is fine in PCRE and does not capture

Comments