Solved

Regular expression help

Posted on 2013-11-11
11
225 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 48

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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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,…
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now