Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex word matching and extracting next word

Tags:

regex

Can someone help me with this regex expression. I want to extract first word after Manufacturer Part Number:, in this case it's laml005.

The one that nearly does the job is this one: Manufacturer Part Number:(.*)

However, it gets whole line laml005 EAN: 731084217335, and I just need laml005.

String:

about the condition\nBrand: mure\nManufacturer Part Number: laml005 EAN: 731084217335
like image 289
user1423481 Avatar asked Mar 11 '23 14:03

user1423481


1 Answers

Note that .* matches any char other than a newline, and * is a greedy quantifier matching zero or more instances of the construct/symbol before it. So, .* can't but get you the whole rest of the line.

You can use \S+ to only capture 1 or more non-whitespace symbols, or \w+ to match 1 or more word chars after Manufacturer Part Number::

/Manufacturer Part Number:\s*(\S+)/

See this regex demo

like image 85
Wiktor Stribiżew Avatar answered Mar 19 '23 14:03

Wiktor Stribiżew