Solved

Help with regex

Posted on 2008-10-19
6
221 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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…
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.

705 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now