...

View Full Version : RegEx? Will this match?



FutureWebDev
05-23-2012, 10:26 PM
/\w+\sis\s\w+/

I'm beginning to study regular expressions and wanted to know if the above expression would match things like:

Bob is cool
Laura is nice
Jenny is pretty

OR

futurewebdev is wrong :p

Thanks
FWD

Logic Ali
05-23-2012, 11:13 PM
/\w+\sis\s\w+/

I'm beginning to study regular expressions and wanted to know if the above expression would match things like...

What's stopping you from testing it?

FutureWebDev
05-23-2012, 11:38 PM
What's stopping you from testing it?

Because I'm reading through this book and wanted to quickly get an idea of whether or not I'm thinking about regular expressions correctly.

Whats stopping you from just answering?

Old Pedant
05-24-2012, 12:01 AM
It takes a heluva lot more time for you to post the question here and wait for somebody to answer (which could well be hours or even days later) than it does for you to simply test it.

There are *TONS* or regular expression testers out on the web, all free, so you don't even have to right one yourself.

What were you doing in the hour and 12 minutes between your first post and second? Couldn't you have tested it about 30 times over in that time?

Yes, it would match all of those. And a lot of much simpler ones too.
All you are asking for is to find "is", with a space on each side, and a letter or number or underline on each side of that.

_ is _ would match
9 is 1 would match
w is _ would match
and so on.

low tech
05-24-2012, 12:26 AM
Hi FutureWebDev

For me regx is the area I read, I understand, I apply it myself, It looks right (to me) and it never works as expected hahahahaha

Anyway, try these tools. Maybe not perfect, but I find useful.

http://regexpal.com/

http://www.gskinner.com/RegExr/

LT

FutureWebDev
05-24-2012, 12:38 AM
It takes a heluva lot more time for you to post the question here and wait for somebody to answer (which could well be hours or even days later) than it does for you to simply test it.

There are *TONS* or regular expression testers out on the web, all free, so you don't even have to right one yourself.

What were you doing in the hour and 12 minutes between your first post and second? Couldn't you have tested it about 30 times over in that time?

Yes, it would match all of those. And a lot of much simpler ones too.
All you are asking for is to find "is", with a space on each side, and a letter or number or underline on each side of that.

_ is _ would match
9 is 1 would match
w is _ would match
and so on.

Or in the 5 min it took you to answer my question to somebody else for the sake of living up to your title of "Supreme Coder'", you could have simply replied with the last two paragraphs of your response.

Or you could have suppressed your temptation to make me out to be an idiot, and actually came to the conclusion that: 1.) I'm new to regex, 2.) I am unaware of the simplicity involved with testing regular expressions.

If you were to test those two conclusions they both would have resulted in TRUE.

But thanks at least for answering.
FWD

Philip M
05-24-2012, 11:29 AM
Yes, it will match, but it will match many other patterns as well, say 567Jen789ny is nice%&$. What you want is for the string and nothing else to match the pattern. \w means matches word characters (letters, digits, and underscores). Note that ny is nice or 23 is 99 matches your regex. It is often not as easy as that to test a regex comprehensively - you must ensure that the pattern does not match invalid strings as well as testing that it does match valid ones. I think that Old Pedant was rather unkind here.



<script type="text/javascript">

var str = "Jenny is nice";

if (/^[a-z]+\sis\s[a-z]+$/gi.test(str)) { // note the ^ and $ characters which indicate the complete string - the string and nothing else must match the pattern.
alert ("It matched!");
}
else {
alert ("It did not match");
}

</script>

To make this clear, consider the case where you want to match a string consisting of one or more digits. Work out for yourself why the regex will
pass invalid strings such as "1bc23xy" :-


<script type="text/javascript">

var str = "123";
if (/[0-9]+/.test(str)) { // match one or more digits
alert ("It matched!");
}
else {
alert ("It did not match");
}

</script>

The problem is that the regex tests for the presence of one or more digits in the string, but does not test for nothing but one or more digits.



You can test your regular expressions at: http://www.claughton.clara.net/regextester.html



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum