Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    May 2012
    Posts
    30
    Thanks
    9
    Thanked 0 Times in 0 Posts

    RegEx? Will this match?

    /\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

    Thanks
    FWD

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by FutureWebDev View Post
    /\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?

  • #3
    New Coder
    Join Date
    May 2012
    Posts
    30
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Logic Ali View Post
    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?

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,561
    Thanks
    80
    Thanked 4,495 Times in 4,459 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    852
    Thanks
    173
    Thanked 94 Times in 94 Posts
    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
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • Users who have thanked low tech for this post:

    FutureWebDev (05-24-2012)

  • #6
    New Coder
    Join Date
    May 2012
    Posts
    30
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    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

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,147
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    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.


    Code:
    <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" :-

    Code:
    <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
    Last edited by Philip M; 05-24-2012 at 11:50 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •