?
Solved

Need regex: Extract digits following a comma

Posted on 2008-06-24
3
Medium Priority
?
659 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 84

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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…
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…
Suggested Courses

752 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