Regex is driving me crazy!

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
bf2mad
Forum Newbie
Posts: 3
Joined: Mon Dec 14, 2009 8:09 am

Regex is driving me crazy!

Post 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!!!!!!
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Regex is driving me crazy!

Post by AbraCadaver »

Should work:

Code: Select all

#^[^a-Z]{2}#
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Regex is driving me crazy!

Post by prometheuzz »

Try this:

Code: Select all

'/^(?=.?[^a-zA-Z]).*/'
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Regex is driving me crazy!

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Regex is driving me crazy!

Post 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:
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
bf2mad
Forum Newbie
Posts: 3
Joined: Mon Dec 14, 2009 8:09 am

Re: Regex is driving me crazy!

Post by bf2mad »

Thanks for the replies

prometheuzz you are a legend, that worked perfect :mrgreen: :mrgreen: :mrgreen:
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Regex is driving me crazy!

Post by prometheuzz »

AbraCadaver wrote:...

You are correct. I hereby restrict myself from posting before noon on a Monday. :oops:
:wink:
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Regex is driving me crazy!

Post 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.
bf2mad
Forum Newbie
Posts: 3
Joined: Mon Dec 14, 2009 8:09 am

Re: Regex is driving me crazy!

Post by bf2mad »

I think I get it, but a short explanation could really help me in future
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Regex is driving me crazy!

Post 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.
MichaelR
Forum Contributor
Posts: 148
Joined: Sat Jan 03, 2009 3:27 pm

Re: Regex is driving me crazy!

Post 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
Post Reply