SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    preg_match & You have GOT to be kidding!

    I did a search for "preg_match" and NOTHING came up. So, I guess mine will be the first!

    I am pulling my hair out trying to figure out this regular expression.

    I am trying to match a string enclosed in quotes, such as:

    "foo"
    &
    "foo bar"
    &
    "any number of words enclosed in quotes"

    This is the regular expression that matches one ONE word, I can't make it match when there is more than one word between the quotes:

    if(!preg_match("/^\"\w+\"$/", $keywords))

    Any suggestions?
    John

  2. #2
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    replace \w with . and it should work.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  3. #3
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DR_LaRRY_PEpPeR
    replace \w with . and it should work. [img]images/smilies/smile.gif[/img]
    Nope.

    if(!preg_match("/^\".\"$/", $keywords))
    John

  4. #4
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by PHP John
    Nope.

    if(!preg_match("/^\".\"$/", $keywords))
    oops... forgot the "+"

    But, if preg match is supposed to be PERL regular expressions, why doesn't the \w+ work?
    John

  5. #5
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i didn't say to remove the +

  6. #6
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh, you noticed.

    preg does support Perl regex (mostly) and \w does work. \w only matches word characters, however, not spaces, etc.

    . matches anything and would be the same as

    [\w\W]

    \W = non-word characters.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  7. #7
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DR_LaRRY_PEpPeR
    oh, you noticed. [img]images/smilies/biggrin.gif[/img]

    preg does support Perl regex (mostly) and \w does work. \w only matches word characters, however, not spaces, etc.

    . matches anything and would be the same as

    [\w\W]

    \W = non-word characters. [img]images/smilies/smile.gif[/img]

    Ah! So, just a \w would only match one "word" character, and that is why it only worked on one word when it was \w+, because as soon as it encountered that space and no " it quit...

    Thanks!
    John

  8. #8
    SitePoint Enthusiast Analyser's Avatar
    Join Date
    Oct 2002
    Location
    Delft, The Netherlands
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why don't you say what you really mean? The way I see it, you want a double-quote, followed by a whole lot of non-double-quotes, followed by a double-quote:

    Code:
    /"[^"]+"/
    This does not cater for escaped double-quotes (\"), but then again, none of the provided solutions do
    "A poor programmer is he who blames his tools."
    http://analyser.oli.tudelft.nl/

  9. #9
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My solution in a thread I started a week or so ago handled \".
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  10. #10
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DR_LaRRY_PEpPeR
    . matches anything and would be the same as
    [\w\W]
    \W = non-word characters.
    <nitpicking >
    "[\w\W]" matches newlines
    "." does not unless you use the /s pattern modifier
    </nitpicking>
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  11. #11
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cyborg from dh
    My solution in a thread I started a week or so ago handled \".
    I assume you mean this:
    http://www.sitepointforums.com/showp...34&postcount=8
    Code:
     preg_match('/("|\')([^\1\\\]*?(?:\\\.[^\1\\\]*?)*)\1/s', $str, $result);
    Looks like it works pretty well... I just made about a dozen attempts to create something more simple and failed. Supporting escaped backslashes really complicates things!
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  12. #12
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,067
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I always use the code Analyser posted - by far the best way (in my opinion) of writing an expression like that is to use "lots of things that aren't a quote" or similar. For example, if you want to match an XML style tag you can use the following:

    /<[^>]+>/

  13. #13
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What if you had this:

    <xml:tag attrib="10 is > 1" />
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  14. #14
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by samsm
    <nitpicking >
    "[\w\W]" matches newlines
    "." does not unless you use the /s pattern modifier
    </nitpicking>
    yep, yep. i didn't clarify that.


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
  •