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 to the CF scene
    Join Date
    Jan 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need a regular expression for this..

    I really suck with regular expressions so wondering if any pro's can help me out.

    Here's the string format I'm trying to match:

    {$alphanumeric}

    So it starts with {$ and ends with a } in between will be a random alphanumeric label.

    PHP Code:

    <?php
    $string 
    "{$alphanumericStringHere}";
    $reg ="/({)($)(?=.*[a-z])(})/"

    if (
    preg_match($reg$string)) {
        echo 
    "Matched";
    } else {
        echo 
    "Unmatched";
    }
    ?>
    thanks

  • #2
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    %\{\$([\da-z]+)\}%i
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #3
    New to the CF scene
    Join Date
    Jan 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by JAY6390 View Post
    %\{\$([\da-z]+)\}%i
    Doesn't work

    PHP Code:

    $string 
    "{$abc}";
    $reg ="%\{\$([\da-z]+)\}%i"

    if (
    preg_match($reg$string)) {
        echo 
    "Matched";
    } else {
        echo 
    "Unmatched";

    result: Unmatched

  • #4
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    The problem isn't the regex, it's your double quotes around the text. Changing it to single quotes will work. The reason is that PHP tries to substitute $abc for the variable $abc's value, which doesn't exist.
    This works..
    PHP Code:
    $string '{$aaBBcc112233CCbbAA}';
    $reg '%\{\$([\da-z]+)\}%i';

    if (
    preg_match($reg$string)) {
        echo 
    "Matched";
    } else {
        echo 
    "Unmatched";

    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Looks ok offhand. Been awhile, and although it shouldn't make a difference, I question if the { and } need escaping in this pattern since the limiter wouldn't be valid. The only thing I can suggest is to replace the [\da-z]+ with \w+ to see if that works. \w will however include the _ in it as well, but if it works it may just be some wierdness with the \d to a chain. That could be solved with a [0-9a-z]+ pattern instead. That said, variables cannot start with a number, so if you're planning on later using an eval, you'll need to run it against: [a-z][\w]+

    Edit:
    Ahah, good catch with the double quotes for expansion!
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #6
    New to the CF scene
    Join Date
    Jan 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ahhhhhhhhh!! Thanks very much Jay!

  • #7
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    No problem. This is why error reporting should be on fully during development It would have told you the problem immediately
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)


  •  

    Posting Permissions

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