?
Solved

Regular expression help

Posted on 2013-11-11
11
Medium Priority
?
241 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
[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
  • 2
  • +4
11 Comments
 
LVL 52

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 84

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
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
Course of the Month15 days, 16 hours left to enroll

743 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