Solved

Perl (preg) Regular Expression

Posted on 2004-10-11
4
1,057 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

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…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

728 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