Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

Thread: a slightly better PCRE question

  1. #11
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    1. How is it that '/[^\d-]*(-)[^\d-]*/' is not greedy?
    It is greedy, but it can't match hyphens because they're included in the negative character class.
    2. In the following 3-,-,,-,,,-,,,,-,,,,,-4,5 what is the last match? I guess I am still trying to wrap my mind around the 0 or more quantifier.
    For which pattern?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  2. #12
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Code:
    '/[^\d-]*(-)[^\d-]*/'
    for this one as applied to 3-,-,,-,,,-,,,,-,,,,,-4,5.

    It is greedy, but it can't match hyphens because they're included in the negative character class.
    Sorry, dumb question, but if it is ungreedy, wouldn't it replace the 4 as well? The way I understand the above expression it will match everything between the first [^\d-]* and the last [^\d-]*.

  3. #13
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    The last match will be ",,,,,-".
    Sorry, dumb question, but if it is ungreedy, wouldn't it replace the 4 as well?
    If it were ungreedy it definitely wouldn't. As it stands, it can't -- 4 matches one of the characters in the negative character class.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #14
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    I misspoke (sp?) I meant greedy. Getting sleepy I am afraid. If it is greedy wouldn't it match 3-,-,,-,,,-,,,,-,,,,,-4,-5 and everything in between? or does the negative class automatically apply to that which is between the two matches.

    I changed the example slightly.

    EDIT: wait. Let me try something. I don't think I thought it through before speaking.
    I am currently studying the difference between [].*[] and [].*?[] and [].*?[]? and [].*[]? Once I understand that I will know the answer to my own question.
    Last edited by james438; 08-18-2007 at 09:06 AM.

  5. #15
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    does the negative class automatically apply to that which is between the two matches.
    You're looking at it the wrong way. * means repeat: everything contained therein is part of the match, and has to match the repeated expression. If you have /ba*b/ it's not the same as /ba.*ab/. It can't match ",-,,-,,,-,,,,-,,,,,-4,-" because that contains more than one hyphen (the hyphen isn't repeated in the expression; the hyphen in the negative character class exists for this purpose) and a digit, which it cannot match under any circumstances.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #16
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    yeah... I know. I am kinda embarrassed at my questions really. I am going back to some of the basics of regexp again and practicing some basic examples for a bit till I can do this a little faster in my head.

    I really just need to work on this on my own for a bit.

Bookmarks

Posting Permissions

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