Solved

Perl (preg) Regular Expression

Posted on 2004-10-11
4
1,059 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

617 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