Solved

Perl (preg) Regular Expression

Posted on 2004-10-11
4
1,056 Views
Last Modified: 2013-12-12
                       Need to match anythign like the following:
                        Firstname Lastname [Y] (sdsdd 45)
                        Firstname Lastname (sdsdd 45)
                        Firstname Lastname [R] (sdsdd 45)
                        Firstname Lastname [Y][R] (sdsdd 45)

                        This is for my football site I am trying to develop were [Y] and [R] are yellow and
                        red cards respectively.  Anything brackets reprsents a substitution.
                        I have the following that only partially works:
                        
                        $pattern = '/'.$player.' \((.*?)\)/mis';
                        
                        This matches all players that have not been given a card but have been substituted.  This however won't work:
                        
                        $pattern = '/'.$player.' (\[Y|R\])* \((.*?)\)/mis';
                        
                        It is meant to detect players that may or may not have been given a card but have been substituted.  
                        
                        Any ideas gratefully recieved.
0
Comment
Question by:Ape
  • 2
  • 2
4 Comments
 
LVL 48

Expert Comment

by:hernst42
ID: 12275750
You ned to put a subpattern () arount the Y|R else this means for the regecx
[Y or R] but not [(Y or R)]
$pattern = '/'.$player.' (\[(Y|R)\])* \((.*?)\)/mis';
or write it as
$pattern = '/'.$player.' (\[[YR]\])* \((.*?)\)/mis';

if ue it for a preg_replace you will get the last macht (R) if both Y and R are present.
0
 

Author Comment

by:Ape
ID: 12276461
Thanks.

 That still doesn't work.  For some reason using that it doesn't match on anything: Two examples.

firstname lastname (newplayer 33)
firstname lastname [Y] (newplayer 58)

0
 
LVL 48

Accepted Solution

by:
hernst42 earned 250 total points
ID: 12276766
Here is how I tested it:

$player = 'Firstname Lastname';
$str = 'Firstname Lastname [Y][R] (sdsdd 45)';
$pattern = '/'.$player.'\s*(\[(Y|R)\])* \((.*?)\)/mis';

preg_match($pattern, $str, $m);
var_dump($m);
0
 

Author Comment

by:Ape
ID: 12277540
Sorry it was the matchign parameter in preg_match that I wasn't paying attention to.

Thanks for your help.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question