Regex is driving me crazy!
Moderator: General Moderators
Regex is driving me crazy!
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!!!!!!
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!!!!!!
- 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!
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.
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: Regex is driving me crazy!
Try this:
Code: Select all
'/^(?=.?[^a-zA-Z]).*/'- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: Regex is driving me crazy!
No, it does not. And for two reasons:AbraCadaver wrote:Should work:Code: Select all
#^[^a-Z]{2}#
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
- 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!
You are correct. I hereby restrict myself from posting before noon on a Monday.prometheuzz wrote:No, it does not. And for two reasons:AbraCadaver wrote:Should work:Code: Select all
#^[^a-Z]{2}#
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
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.
Re: Regex is driving me crazy!
Thanks for the replies
prometheuzz you are a legend, that worked perfect

prometheuzz you are a legend, that worked perfect
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: Regex is driving me crazy!
AbraCadaver wrote:...
You are correct. I hereby restrict myself from posting before noon on a Monday.
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: Regex is driving me crazy!
No problem. I assume you understand how (and/or why) it works? If not, just ask for a short explanation.bf2mad wrote:Thanks for the replies
prometheuzz you are a legend, that worked perfect![]()
![]()
Re: Regex is driving me crazy!
I think I get it, but a short explanation could really help me in future
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: Regex is driving me crazy!
Never be afraid to ask when something is not clear.bf2mad wrote:I think I get it, but a short explanation could really help me in future
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 breaksHTH.
Re: Regex is driving me crazy!
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:
Or even the following (but checking for the negative):
Code: Select all
/^(?=.?[^a-z])/iCode: Select all
/^[a-z]{2}/i