?
Solved

Help with regex

Posted on 2008-10-19
6
Medium Priority
?
228 Views
Last Modified: 2012-05-05
Yea i suck at writing regular expressions. From a string like this:

Foo (Bar)

I need to return Foo and Bar separately.  Thanks.
0
Comment
Question by:qwertq
[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
  • 3
  • 2
6 Comments
 
LVL 3

Expert Comment

by:Scripting_Guy
ID: 22754245
/([a-zA-Z]+) \(([a-zA-Z]+)\)/s
0
 

Author Comment

by:qwertq
ID: 22754275
Thanks for the quick reply. I am having trouble getting it to eval on this string:

Test (Test: Red;Another: + 1.00)

Also, just to check - would it still return $hits[0] if the second part in the () is not there?
0
 
LVL 3

Accepted Solution

by:
Scripting_Guy earned 500 total points
ID: 22754296
this makes it a bit different :) the thing about regex is that you need to define exactly which characters you're expecting before the () and inside the ()

For this string, it has to look like this:

'/([a-zA-Z]+) \(([a-zA-Z0-9:;+. ]+)\)/s

$hits[0] always contains the "whole" hit.

in words, this regexp is looking for:
1. at least one or more characters being a-z or A-Z
2. a space
3. a (
4. at least one or more characters being either a-z or A-Z or 0-9 or : or ; or + or . or space
5. a )

if your string does not match these criteria, the regexp won't find anything and $hits will be an empty array
0
Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

 

Author Comment

by:qwertq
ID: 22754305
Can't you use a * for everything inside the ()?
0
 
LVL 3

Expert Comment

by:Scripting_Guy
ID: 22754315
that would be the dot (.) making the regexp look like this:

'/([a-zA-Z]+) \((.+)\)/s'

however, i'm would not use the dot if there is an other way, because the dot will match some more stuff, e.g. like this

"Foo (Bar) Foo (Bar)"
Now this regexp will find two hits:
1. Foo
2. Bar) Foo (Bar

which is not quite what you might be expecting.
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22754389
$matches[2] would be empty if the string did not contain ( ):
Raw Match Pattern:
([^(]+)(?:\(([^)]*)\))?
 
PHP Code Example:
<?php
$sourcestring="your source string";
preg_match('/([^(]+)(?:\(([^)]*)\))?/',$sourcestring,$matches);
echo "<pre>".print_r($matches,true);
?>
 
$matches Array:
(
    [0] => Test (Test: Red;Another: + 1.00)
    [1] => Test 
    [2] => Test: Red;Another: + 1.00
)

Open in new window

0

Featured Post

Independent Software Vendors: 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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…
Suggested Courses

765 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