[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Regular expression help

Posted on 2013-11-11
11
Medium Priority
?
246 Views
Last Modified: 2013-11-13
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
0
Comment
Question by:SheppardDigital
  • 3
  • 2
  • 2
  • +4
11 Comments
 
LVL 54

Expert Comment

by:Rgonzo1971
ID: 39638891
Hi,

pls try

\((\d+)\)

Open in new window

Regards
0
 
LVL 21

Expert Comment

by:Mazdajai
ID: 39638925
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
 

Author Comment

by:SheppardDigital
ID: 39639572
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
Industry Leaders: 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!

 
LVL 21

Expert Comment

by:Mazdajai
ID: 39639619
^"\((\d+)\).+"$

Open in new window

0
 
LVL 9

Expert Comment

by:Derek Jensen
ID: 39639782
\((\d+)\) products

Open in new window

0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 39639843
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
 
LVL 9

Expert Comment

by:Derek Jensen
ID: 39639860
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
 

Author Comment

by:SheppardDigital
ID: 39641010
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
 

Author Comment

by:SheppardDigital
ID: 39641020
ah sorry, my mistake.

The text I'm actually trying to find is

(123 products)

and not
(123) products
0
 
LVL 85

Expert Comment

by:ozo
ID: 39641200
\((\d+)
0
 
LVL 36

Accepted Solution

by:
mccarl earned 2000 total points
ID: 39641385
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

834 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