porswengr
porswengr

Reputation: 53

regex to match pattern followed some string

I have following text. I want to capture the pattern ddd-dd-ddd followed by all text until I again hit a ddd-dd-ddd. I am trying to use this regex

\b[0-9]{3}-[0-9]{2}-[0-9]{3}\b.*

it matches 982-99-122 followed by the sentence until it hits a line feed. then again the second number 586-33-453 is matched followed by the text on the same line. but it fails to capture the text that continues on the next line.

OR if I remove the line feed from this string, it will only capture the first number 982-99-122 and captures the whole string i.e. does not match the second number 586-33-453

How should I fix both these issues, 1. when line feeds are part of the string and 2. when the string does not have line feeds.

982-99-122 (FCC 333/22) lube oil service pump 1b discharge lube oil service pump  
aaa bb dsdsd  
586-33-453 Matches exactly 3 times 0-e single character in the range between 
dfldfldflkdf 545-66-666 sdkjsl () jdfkjd-kfdkf sdfl  
848-99-040 sdsd"" df  
dfdf  

Upvotes: 1

Views: 50

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626689

It seems you want

\b([0-9]{3}-[0-9]{2}-[0-9]{3})\b([\s\S]*?)(?=\b[0-9]{3}-[0-9]{2}-[0-9]{3}\b|$)?

See the regex demo

Details

  • \b - word boundary
  • ([0-9]{3}-[0-9]{2}-[0-9]{3}) - 3 digits, -, 2 digits, - and 3 digits
  • \b - word boundary
  • ([\s\S]*?) - Group 2: any 0+ chars, as few as possible
  • (?=\b[0-9]{3}-[0-9]{2}-[0-9]{3}\b|$)? - a positive lookahead that requires 3 diigts, -, 2 digits, - and 3 digits as a whole word or end of string immediately to the right of the current location.

Upvotes: 1

Related Questions