PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Thu Jul 18, 2019 3:15 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Fri Oct 14, 2011 12:41 am 
Offline
Forum Newbie

Joined: Wed Mar 11, 2009 8:54 am
Posts: 10
Hi All,

I have a pattern like this:

RS-1.75 m(TE)

from which, I want to select string from round brackets ( in this case, TE). Can anyone help me in regex or any other way to do this?

Thanks,


Top
 Profile  
 
PostPosted: Mon Oct 17, 2011 6:12 pm 
Offline
Forum Commoner

Joined: Sun Oct 09, 2011 6:54 pm
Posts: 35
Location: London
Syntax: [ Download ] [ Hide ]
$string = "This is a (IE) test about the this (UY)";
preg_match_all("/\((.*)\)/U",$string,$results);


foreach ($results[1] as $ID ) print $ID . "<BR>";


Top
 Profile  
 
PostPosted: Wed Dec 28, 2011 5:36 pm 
Offline
Forum Newbie

Joined: Wed Dec 28, 2011 5:33 pm
Posts: 1
Thank you for this response. I had the very same question. This looks like it would work, but I am a little confused about the /U modifier. It seems that this would cause some problems, I might be wrong about this. Is there another approach that I can take in this situation, or is this the best way? I will need to work this issue out before I can move forward on the software that I have under development. As of now, this little snippet is causing bugs all over the place, so hopefully someone has some helpful advice. Thank you.


Last edited by Tosha on Wed Jan 04, 2012 10:20 am, edited 1 time in total.

Top
 Profile  
 
PostPosted: Wed Dec 28, 2011 5:57 pm 
Offline
Forum Commoner
User avatar

Joined: Thu Dec 15, 2011 2:40 pm
Posts: 85
Location: Nelson, NZ
Yes, ouchiko's pattern works, but...
I would not recommend it.
Why? Because it uses an uncommon (and potentially confusing) modifier: /U
This pattern modifier says "swap the greediness status of the star and question mark quantifiers". So in this expression, the "greedy star" is lazy!!! That means you have a non-standard regex that will break almost everywhere except in this particular context.

Making only slight modifications to ouchiko's code, I would suggest something much more conventional:
Syntax: [ Download ] [ Hide ]
<?php
$string = "This is a (IE) test about the this (UY)";
preg_match_all(",\(([^)]*)\),",$string,$results);
foreach ($results[1] as $ID ) print $ID . "<br />";
?>


In the capturing parentheses, instead of capturing everything, we capture anything that is not a closing parenthesis.

Note: This is only a starting point on the wide topic of capturing the content inside parentheses. For nested sets of parentheses, you may have to use a .

Wishing you all a beautiful end of the year.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group