Regular expressions help needed

Discussion in 'PHP' started by Istvan, Aug 29, 2008.

  1. #1
    Hi! I'm a real Regular Expression noob but I need this for a project.

    I need to convert a string to variables. The string is composed by a phrase (one, two or three words), one number, a separator, one number, a phrase. So for example: South Africa 1-1 Argentina

    To "separate" the things I should use (correct me if I'm wrong) the ereg function...what is the regular expression that match that phrase? :confused:

    Thanks!
     
    Istvan, Aug 29, 2008 IP
  2. ahowell

    ahowell Peon

    Messages:
    38
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It would help to know how you would like it split up ;)

    Give an example output that you would like it to end up as.
     
    ahowell, Aug 29, 2008 IP
  3. gustavorg

    gustavorg Active Member

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    61
    #3
    Do you want something like

    $var = (
    0 => "South Africa",
    1 => "1",
    2 => "1",
    3 => "Argentina"
    )
     
    gustavorg, Aug 29, 2008 IP
  4. Istvan

    Istvan Well-Known Member

    Messages:
    1,544
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    175
    #4
    Yep sorry forgot about that :)
    The final result should be: South Africa-Argentina 1-1

    Mean while I have (probably) found the correct RegEx:
    if (ereg ("([a-zA-Z ]+) ([0-9]{1,2})-([0-9]{1,2}) ([a-zA-Z ]+)", $title, $regs)) {...
    Code (markup):
    EDIT: @gustavorg: yes someting like that
     
    Istvan, Aug 29, 2008 IP
  5. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #5
    Stay away from "ereg_*". Use "preg_*" instead. You can think of the ereg functions as depreciated preg functions.

    For this situation, the syntax will be about the same. you'll need to place pattern delimiters on either end of the pattern though. That pretty much consists of adding a # or ~ to the beginning and end of the pattern.

    http://www.php.net/preg_match
     
    joebert, Aug 29, 2008 IP
  6. Dman91

    Dman91 Peon

    Messages:
    46
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Why not use explode ?
     
    Dman91, Aug 30, 2008 IP
  7. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You can use
    ^(?P<team1>[A-Za-z\s]+) (?P<score1>\d+)\-(?P<score2>\d+) (?P<team2>[A-Za-z\s]+)$
    Code (markup):
    which then even names the array keys correctly
    so for example
    $result = "South Africa 1-1 Argentina";
    $pattern = '/(?P<team1>[A-Za-z\s]+) (?P<score1>\d+)\-(?P<score2>\d+) (?P<team2>[A-Za-z\s]+)/';
    preg_match($pattern,$result,$out);
    
    echo '<pre>'.print_r($out, true).'</pre>';
    PHP:
    As you can see, $out['team1'] is South Africa etc. And like joebert said, stay away from eregi

    Jay
     
    JAY6390, Aug 30, 2008 IP
  8. Istvan

    Istvan Well-Known Member

    Messages:
    1,544
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    175
    #8
    Thanks for the replies.
    Does ereg and preg use the same syntax? So, can I substitute ereg with preg_match in the string I have wrote in the previous post or it won't work? I'm asking this because I have use ereg in many files, and now it's a bit hard to recode the whole thing.

    Thanks
     
    Istvan, Sep 6, 2008 IP
  9. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Yes they have the same syntax (with extra arguments optional in preg_match that you wont need for your example). You will need to add a delimeter to either side of your regular expressions though,
    so
    if (ereg ("([a-zA-Z ]+) ([0-9]{1,2})-([0-9]{1,2}) ([a-zA-Z ]+)", $title, $regs))
    
    //becomes
    
    if (preg_match("/([a-zA-Z ]+) ([0-9]{1,2})-([0-9]{1,2}) ([a-zA-Z ]+)/", $title, $regs))
    PHP:
    Note the / on either side of the pattern now
     
    JAY6390, Sep 6, 2008 IP
    Istvan likes this.