regex : stop at first match move onto next line
Discussion
A thousand humble apologies for such a basic question. This is new to me as of last night.
I have a list of product descriptions, and part of that tells me how many units are in a multipack. I want to get a list of the units.
eg
2PK TEA TOWELS
3 PK BOG ROLL
4PK BINBAGS 4PK
100PK TOOTHPICKS
I want to return a list that is ...
2
3
4
100
What I actually get, using \d+(?=\s?PK) is ...
2
3
4
4 .. because it's picking up the 2nd '4PK' in that line
100
I want to have the expression find the first match in the line and then move onto the next line, not continue to the end and pick up any other matches. I've been reading up on lazy & greedy selectors and it might as well all be in Chinese for me at the moment
Thanks in advance. I'm using regexr.com to test, if that matters.
I have a list of product descriptions, and part of that tells me how many units are in a multipack. I want to get a list of the units.
eg
2PK TEA TOWELS
3 PK BOG ROLL
4PK BINBAGS 4PK
100PK TOOTHPICKS
I want to return a list that is ...
2
3
4
100
What I actually get, using \d+(?=\s?PK) is ...
2
3
4
4 .. because it's picking up the 2nd '4PK' in that line
100
I want to have the expression find the first match in the line and then move onto the next line, not continue to the end and pick up any other matches. I've been reading up on lazy & greedy selectors and it might as well all be in Chinese for me at the moment
Thanks in advance. I'm using regexr.com to test, if that matters.
Could look for text after the "PK"...
\d+(?=\s*PK.{4,})
(Doing this on my phone while on a conference call... Should need 4 characters after PK, but might have problems with multiline/single line).
[^\n]{4,} if there are problems
Eta: probably overcomplicated it.
I would do:
(\d+)\s*PK\s+[^\n]+
And pick up the first group.
\d+(?=\s*PK.{4,})
(Doing this on my phone while on a conference call... Should need 4 characters after PK, but might have problems with multiline/single line).
[^\n]{4,} if there are problems
Eta: probably overcomplicated it.
I would do:
(\d+)\s*PK\s+[^\n]+
And pick up the first group.
Edited by tribbles on Friday 10th February 10:12
To easily try regex expressions out I recommend www.regex101.com
Gassing Station | Computers, Gadgets & Stuff | Top of Page | What's New | My Stuff