Solved

Need regex: Extract digits following a comma

Posted on 2008-06-24
3
627 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
Comment Utility
s/,.*//

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

Assisted Solution

by:ghostdog74
ghostdog74 earned 250 total points
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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

763 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

10 Experts available now in Live!

Get 1:1 Help Now