Solved

Help with regex

Posted on 2008-10-19
6
226 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

732 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