Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Need regex: Extract digits following a comma

Posted on 2008-06-24
3
Medium Priority
?
667 Views
Last Modified: 2012-05-05
Hi experts,

I have a list of entries like so:

Product XYZ ABC, 30 items
Product AS, 5 items
AS AZSU, 3
...

I need to regular expressions:

1) I only want to keep what comes before the comma, thus "Product XYZ ABC", "Product AS"... The comma and everything after can be discarded.

2) One regex that extracts the number after the comma, i.e. only "30", "5" or "3".

Thank you for your help!
0
Comment
Question by:Lupi05
3 Comments
 
LVL 85

Accepted Solution

by:
ozo earned 750 total points
ID: 21854287
s/,.*//

s/.*,\s*(\d+).*/$1/
0
 
LVL 9

Assisted Solution

by:ghostdog74
ghostdog74 earned 750 total points
ID: 21855140
no need regular expression, just split on comma


awk 'BEGIN { FS=","}
{
 print $1 #print your product
 n=split($2,a," ")
 print a[1] #print your number
}' file

Open in new window

0
 
LVL 27

Expert Comment

by:ddrudik
ID: 21855719
If your app/lang supports regex lookbehind (?<=):

Raw Match Pattern:
(?<=,).*

$matches Array:
(
    [0] => Array
        (
            [0] =>  30 items
            [1] =>  5 items
            [2] =>  3
        )

)

If not:

Raw Match Pattern:
,(.*)

$matches Array:
(
    [0] => Array
        (
            [0] => , 30 items
            [1] => , 5 items
            [2] => , 3
        )

    [1] => Array
        (
            [0] =>  30 items
            [1] =>  5 items
            [2] =>  3
        )

)

And for your second requested pattern:

Raw Match Pattern:
, *(\d+)

$matches Array:
(
    [0] => Array
        (
            [0] => , 30
            [1] => , 5
            [2] => , 3
        )

    [1] => Array
        (
            [0] => 30
            [1] => 5
            [2] => 3
        )

)

For this I chose a regex match all method instead of regex replace but either would suffice.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
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…

916 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