Solved

Need regex: Extract digits following a comma

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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

863 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

21 Experts available now in Live!

Get 1:1 Help Now