Solved

Regular expression help

Posted on 2013-11-11
11
227 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 49

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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JavaScript date form validation 6 53
Date input validation (Regular Expressions) 1 45
Two RegEx conditions in one 3 46
Regular Expression Calculator Tester 2 74
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…
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…

810 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