Page 1 of 1

Regex is driving me crazy!

Posted: Mon Dec 14, 2009 8:22 am
by bf2mad
I need some help creating a regex expression.

The expression must match if there is anything other than a lowercase or upercase letter in either the first or second position of a string

For example the following should be matched;

a7the_rest_of_the_string
7athe_rest_of_the_string
77the_rest_of_the_string
a$the_rest_of_the_string
&7the_rest_of_the_string
Z%the_rest_of_the_string

And these shouldn't;

aa77the_rest_of_the_string
aZ7$the_rest_of_the_string

So far all I have is

^[^a-zA-Z]

Thanks in advance!!!!!!

Re: Regex is driving me crazy!

Posted: Mon Dec 14, 2009 8:48 am
by AbraCadaver
Should work:

Code: Select all

#^[^a-Z]{2}#

Re: Regex is driving me crazy!

Posted: Mon Dec 14, 2009 8:58 am
by prometheuzz
Try this:

Code: Select all

'/^(?=.?[^a-zA-Z]).*/'

Re: Regex is driving me crazy!

Posted: Mon Dec 14, 2009 9:00 am
by prometheuzz
AbraCadaver wrote:Should work:

Code: Select all

#^[^a-Z]{2}#
No, it does not. And for two reasons:

1 - a-Z is not a valid range, you probably meant [^a-zA-Z]
2 - and [^a-zA-Z]{2} would only match if the string starts with two characters other than a-z or A-Z while the OP also wants to match the string if one of them is not a a-z or A-Z

Re: Regex is driving me crazy!

Posted: Mon Dec 14, 2009 9:42 am
by AbraCadaver
prometheuzz wrote:
AbraCadaver wrote:Should work:

Code: Select all

#^[^a-Z]{2}#
No, it does not. And for two reasons:

1 - a-Z is not a valid range, you probably meant [^a-zA-Z]
2 - and [^a-zA-Z]{2} would only match if the string starts with two characters other than a-z or A-Z while the OP also wants to match the string if one of them is not a a-z or A-Z
You are correct. I hereby restrict myself from posting before noon on a Monday. :oops:

Re: Regex is driving me crazy!

Posted: Mon Dec 14, 2009 10:26 am
by bf2mad
Thanks for the replies

prometheuzz you are a legend, that worked perfect :mrgreen: :mrgreen: :mrgreen:

Re: Regex is driving me crazy!

Posted: Mon Dec 14, 2009 10:29 am
by prometheuzz
AbraCadaver wrote:...

You are correct. I hereby restrict myself from posting before noon on a Monday. :oops:
:wink:

Re: Regex is driving me crazy!

Posted: Mon Dec 14, 2009 10:30 am
by prometheuzz
bf2mad wrote:Thanks for the replies

prometheuzz you are a legend, that worked perfect :mrgreen: :mrgreen: :mrgreen:
No problem. I assume you understand how (and/or why) it works? If not, just ask for a short explanation.

Re: Regex is driving me crazy!

Posted: Mon Dec 14, 2009 10:43 am
by bf2mad
I think I get it, but a short explanation could really help me in future

Re: Regex is driving me crazy!

Posted: Mon Dec 14, 2009 11:12 am
by prometheuzz
bf2mad wrote:I think I get it, but a short explanation could really help me in future
Never be afraid to ask when something is not clear.

Here you go:

Code: Select all

^                 # match the start of the string
(?=               # start positive look ahead
    .?            #     match any character (except line breaks) zero or one time
    [^a-zA-Z]     #     match a single character other than in the range a-z and A-Z
)                 # stop positive look ahead
.*                # match zero or more characters other than line breaks
In other words: at the start of the string, there should be a character other than in the range a-z and A-Z either on the first, or second spot.

HTH.

Re: Regex is driving me crazy!

Posted: Wed Dec 16, 2009 11:45 am
by MichaelR
If you want to be really anal, you can use the following to save on a few characters (note that .* is not required because no end of line anchor ($) is given:

Code: Select all

/^(?=.?[^a-z])/i
Or even the following (but checking for the negative):

Code: Select all

/^[a-z]{2}/i