Solved

Regular expression help

Posted on 2013-11-11
11
230 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 51

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
Independent Software Vendors: 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 35

Accepted Solution

by:
mccarl earned 500 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
A regular expression for email address 6 77
Regular expression patterns 2 95
RegEx with optional part 4 81
Need a simple RegEx to search for two words 27 123
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…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
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

737 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