Solved

Perl (preg) Regular Expression

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
The viewer will learn how to count occurrences of each item in an array.
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now