Solved

Help with regex

Posted on 2008-10-19
6
227 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 125 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

688 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