Solved

Help with regex

Posted on 2008-10-19
6
224 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
  • 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 

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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

821 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