regex : stop at first match move onto next line

regex : stop at first match move onto next line

Author
Discussion

bigandclever

Original Poster:

13,924 posts

244 months

Friday 10th February 2023
quotequote all
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 smile

Thanks in advance. I'm using regexr.com to test, if that matters.


tribbles

4,016 posts

228 months

Friday 10th February 2023
quotequote all
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.

Edited by tribbles on Friday 10th February 10:12

jesusbuiltmycar

4,620 posts

260 months

Friday 10th February 2023
quotequote all
To easily try regex expressions out I recommend www.regex101.com

bigandclever

Original Poster:

13,924 posts

244 months

Saturday 11th February 2023
quotequote all
Cheers team! I ended up getting the ache with it and did a quick Alteryx job to bring back only the first matches.