Home > Back-end >  REGEX getting specific string and numbering
REGEX getting specific string and numbering

Time:09-29

I have the following txt file:

VIII.  LOCATIONS:  OLA Handbook 1300.8, OPEN ARMS
                OLA Handbook 1300.21, BIG BROTHER
                

9.  RESTAURANTS:  ARBYS 22RX-60-81, same subject, dated March 20, 2018.

But I want to match anywhere with OLA Handbook and the numbers after it. But also be under the Locations section (so text after LOCATIONS:)

For example expected output would be: How can i do this in regex?

 OLA Handbook 1300.8
 OLA Handbook 1300.21

SO far I've only done

(?=LOCATIONS).*$

But I'm confused how to proceed.

I'm using https://regex101.com/ to test my string and the txt file is from notepad .

CodePudding user response:

In notepad you can make use of the \G anchor to get continuous matches.

(?s)(?:\bLOCATIONS:|\G(?!^)).*?\b\KOLA\s Handbook\s \d (?:\.\d )\b

In parts, the pattern matches:

  • (?s) Inline modifier, dot matches newline
  • (?: Non capture group
    • \bLOCATIONS: Match literally, preceded by a word boundary to prevent a partial match
    • | Or
    • \G(?!^) Assert the position at the end of the previous match, not at that start
  • ) Close non capture group
  • .*? Match as least as possible chars
  • \b\K A word boundary, then use \K to clear the current match buffer (forget what is matched until now)
  • OLA\s Handbook\s Match the text, where \s matches 1 or more whitespace chars
  • \d (?:\.\d )\b Match 1 digits and an optional decimal part and a word boundary

Regex demo

CodePudding user response:

Did you try this exhausted code?

Find: (ola.*\d)|.?
Replace all: $1

  • Related