Solved

Need regex: Extract digits following a comma

Posted on 2008-06-24
3
641 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 84

Accepted Solution

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

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

Assisted Solution

by:ghostdog74
ghostdog74 earned 250 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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…
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…

820 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