Solved

Perl (preg) Regular Expression

Posted on 2004-10-11
4
1,055 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

839 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