Regular expression help

Hi,

Should be an easy one for someone that understands regular expressions.

I have the text...
(123) products

I need a regular expression that will return the number within the brackets, this number could be any length, ie.1 or 12345
SheppardDigitalAsked:
Who is Participating?
 
mccarlConnect With a Mentor IT Business Systems Analyst / Software DeveloperCommented:
Ok, so for PHP, this should work...
<?php
$input = "blah blah blah... Toys (123 products)\nblah blah";
preg_match('/\((\d+) products\)/i', $input, $result);
$numProducts = $result[1];

echo "number of products: {$numProducts}\n";
?>

Open in new window

0
 
Rgonzo1971Commented:
Hi,

pls try

\((\d+)\)

Open in new window

Regards
0
 
MazdajaiCommented:
This match beginning of a line follow with a open parentheses. Capture any number of digits. Match a close parentheses and anything afterward.

^\((\d+)\).+$

Open in new window

0
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.

 
SheppardDigitalAuthor Commented:
Hi,

Sorry, I forgot to mention I also need to match it against the word products.

"(123) products"

I need to detect "(?) products" and have it return the value of ?
0
 
MazdajaiCommented:
^"\((\d+)\).+"$

Open in new window

0
 
Derek JensenCommented:
\((\d+)\) products

Open in new window

0
 
Terry WoodsIT GuruCommented:
Try this pattern:
\((\d+)\) products

Open in new window

The number will be captured as the first group. There are different ways of getting the value, depending on the language.

Which language are you using?
0
 
Derek JensenCommented:
I'm still reading up on this, but the most common way I've seen thus far is $1 or \1 to grab the first capturing group (or the first outermost group, if you've got nested parens).
0
 
SheppardDigitalAuthor Commented:
Hi,

bigdogdman, your suggestion kind of works, but when I've applied it to the actual text it's not working.

The text I'm looking for is in the middle of some HTML, but it's returning no results, however if I just apply it to the text '(123) products' then its works fine, but in reality the line of text I'm trying to pull from is 'Toys (123) products'

I'm using PHP by the way.
0
 
SheppardDigitalAuthor Commented:
ah sorry, my mistake.

The text I'm actually trying to find is

(123 products)

and not
(123) products
0
 
ozoCommented:
\((\d+)
0
All Courses

From novice to tech pro — start learning today.