PDA

View Full Version : Regular Expression Help


welshsteve
07-06-2009, 11:40 AM
I am trying to test some strings against a regular expression, but have tried at least 10 different online testers with no success at all. Plus I've tried some code to do it myself, again with no success. I know for a fact that some of the strings should match and some shouldn't match, but I am getting "No match" returns from all the strings.

Does somebody have some page code that has the regular expression in some javascript code in the head section of a document, then a form in the body that I can enter the text string, click a button, and I get an alert saying if the string matches or not?

Horus_Kol
07-06-2009, 07:16 PM
haven't got anything online for you - I use a downloaded app called RegExBuilder for testing (or sometimes I just test using a simple PHP script).

There are actually a lot of "RegExBuilder" apps - and I can't find the one I use in search anymore and there are no site links in the app - but I'd recommend getting one of these apps for testing.

welshsteve
07-07-2009, 04:10 AM
Thanks Horus. A very helpful answer comapred to answers received elsewhere :)

RysChwith
07-07-2009, 08:20 AM
What are the pattern and strings you're using?

Rys

welshsteve
07-07-2009, 09:06 AM
Hi Rys. This was part of an Open University assignment I was given. I submitted the assignment last night so can't change anything now (I wouldn't want to cheat anyway), but I know for sure I got the question wrong (as well as a few others I think).

Here are the strings. I was asked which two would match the regular expression.

Regular Expression
/a*[a-p]+(A@|at)\D*\$(\w|\d)*$/i

Strings
X_Aar@ddddpp$RUT_*Khyu
aAatdffdd24spp$RUT_Khyu
hAatdddoulapp$RUT_Khyu
h.Aatdddoulpp$RUT_Kh24yua
hAa@dddoulpp$RUT_Kh24yu+a

I wanted to try and write a code page to test each string, but I couldn't get any of them to match so assumed I'd done it wrong.

EDIT:This link works perfectly :) - http://www.claughton.clara.net/regextester.html

I'll study the code in there to see if I can understand it.

Jon Hanlon
07-07-2009, 10:13 PM
Try RegExpPal (http://regexpal.com/)

If you're interested in RE's -- and you should be, they're so neat -- check the Flagrant Badassery (http://blog.stevenlevithan.com/) blog. There are links to other testers down the page near the photo of his book.

Horus_Kol
07-08-2009, 07:18 PM
hehe - to quote http://www.codinghorror.com/blog/ :

Some people see a problem and then start using regular expressions. Then they have two problems

As with anything else - always think hard about whether a regex is the best way forward...

paul_norman_81
07-08-2009, 07:52 PM
There's rarely problem that you can't hammer into submission with a good ole regexp... ;)

To help you with this question (late I know...) keep in mind the pattern: /a*[a-p]+(A@|at)\D*\$(\w|\d)*$/i - we can immediately see that it is case insensitive, so ignore case from here on...

1) We know that the string can have zero or more letter a's to begin with, but that doesn't help us, so we move on.

2) There must be 1 or more instance of a letter a-p at the beginning, so the first string is out. 4 remain.

3) All remaining strings qualify for the a@ or at condition.

4) All will qualify for zero or more non-digits, but this moves each to the next digit OR the first dollar sign... The second string fails here as the dollar sign is not directly after the digits. 3 remain.

5) Then you are only allowed word characters (a-z hyphen space underscore etc) or digits in the remaining part. The last string has a + character so will fail.

Thus:

X_Aar@ddddpp$RUT_*Khyu - fail
aAatdffdd24spp$RUT_Khyu - fail
hAatdddoulapp$RUT_Khyu - pass
h.Aatdddoulpp$RUT_Kh24yua - pass
hAa@dddoulpp$RUT_Kh24yu+a - fail

And for future testing should you need it you can use PHP (which I know that you are somewhat familiar with!):


<?php

$pattern = '/a*[a-p]+(A@|at)\D*\$(\w|\d)*$/i';

$string[] = 'X_Aar@ddddpp$RUT_*Khyu';
$string[] = 'aAatdffdd24spp$RUT_Khyu';
$string[] = 'hAatdddoulapp$RUT_Khyu';
$string[] = 'h.Aatdddoulpp$RUT_Kh24yua';
$string[] = 'hAa@dddoulpp$RUT_Kh24yu+a';

foreach($string as $val)
{
echo $val.' - ';
if(preg_match($pattern, $val))
{
echo 'passed';
}
else
{
echo 'failed';
}
echo '<br />';
}

welshsteve
07-09-2009, 07:29 AM
Paul, that's great. Many thanks for that explanation.