regular expression newbee. plz help!!
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: regular expression newbee. plz help!!

  1. #1
    Member
    Join Date
    Aug 2013
    Location
    Manchester, England.
    Posts
    49

    regular expression newbee. plz help!!

    Hi.

    I need some help with the following code:


    PHP Code:
    $myPets “favoritePet=LuckyRover=dogLucky=cat”;

    preg_match/favoritePet\=(\w+).*\1\=(\w+)/$myPets$matches );

    echo 
    “My favorite pet is a “ $matches[2] . “ called “ $matches[1] . .


    This code displays the following output:

    Code:
     “My favorite pet is a cat called Lucky.”

    The thing i can't understand is how the regular expression jumps past the Rover=dog part of the sting.

    could someone explain how it does this?
    Last edited by NogDog; 09-15-2013 at 09:04 PM. Reason: changed quote tags to php and code tags

  2. #2
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    13,943
    Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." ~ from Nation, by Terry Pratchett

    "But the main reason that any programmer learning any new language thinks the new language is SO much better than the old one is because he’s a better programmer now!" ~ http://www.oreillynet.com/ruby/blog/...ck_to_p_1.html


    eBookworm.us

  3. #3
    Member
    Join Date
    Aug 2013
    Location
    Manchester, England.
    Posts
    49
    could u tell me in more detail. yourself? as ive read this link and i seem to be missing something.

  4. #4
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,885
    \1 matches whatever the first parenthesised part matched. \2 matches the second, and so on.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  5. #5
    Senior Member
    Join Date
    Jul 2007
    Posts
    3,657
    While the entire pattern would match nothing in this case. Perhaps this is where the confusion arises?

  6. #6
    Member
    Join Date
    Aug 2013
    Location
    Manchester, England.
    Posts
    49
    could someone just tell me what part of this regular expression tells it to jump past Rover=dog (i understand everything else):

    ‘/favoritePet\=(\w+).*\1\=(\w+)/’

  7. #7
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,428
    ".*" -- any character zero or more times.

  8. #8
    Member
    Join Date
    Aug 2013
    Location
    Manchester, England.
    Posts
    49
    right. i understand what that little snippet means. but how does it jump past Rover=dog,??

    Im guessing it does it in conjunction with something else?

  9. #9
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,428
    Yes, it "skips" Rover=dog because "Rover" is not what was matched in the first subgroup (which is what the backreference "\1" refers to).

  10. #10
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    13,943
    Code:
    /favoritePet\=(\w+).*\1\=(\w+)/
    holds the favorite pet
    skips over stuff until...
    ...it finds the favorite pet's name... (back reference)
    ...at which point it gets that pet's type
    Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." ~ from Nation, by Terry Pratchett

    "But the main reason that any programmer learning any new language thinks the new language is SO much better than the old one is because he’s a better programmer now!" ~ http://www.oreillynet.com/ruby/blog/...ck_to_p_1.html


    eBookworm.us

  11. #11
    Member
    Join Date
    Aug 2013
    Location
    Manchester, England.
    Posts
    49
    ok. i think i get it.. so the word 'Lucky' within the sting is crucial to jumping past Rover=dog?

  12. #12
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,428
    It's not so much that it is "jumping past" the "Rover=dog" -- that part still gets matched by the ".*" because the next part of the pattern is looking for "Lucky=(any word characters one or more times)" due to the backreference. Since the ".*" isn't surrounded by parentheses, it isn't placed in a subgroup - unlike the "any word characters one or more times" that follow "Lucky=".

  13. #13
    Member
    Join Date
    Aug 2013
    Location
    Manchester, England.
    Posts
    49
    through the use of (\w+)........ becoz lucky is stored inside (\w+).

  14. #14
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,428
    Quote Originally Posted by Paul help! View Post
    becoz lucky is stored inside (\w+).
    No; "cat" is matched by the (\w+).

    EDIT: Think I see the confusion here... I thought you were referring to the second instance of (\w+). "Lucky" is indeed matched by the first subgroup... which also happens to be (\w+).
    Last edited by bradgrafelman; 09-16-2013 at 01:16 PM.

  15. #15
    Member
    Join Date
    Aug 2013
    Location
    Manchester, England.
    Posts
    49
    but i thought the back reference \1 refer to Lucky??

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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