Efthymios
Efthymios

Reputation: 263

Get substrings from string using regular expression

I am trying to get sub-strings from a large one using RegEx. The sub-strings' format is as following:

Example sub-strings:

01=#010.0000#001.0000#+10.0#AA_
02=#020.0000#002.0000#+20.0#BB_

Example full string:

01=#010.0000#001.0000#+10.0#AA_02=#020.0000#002.0000#+20.0#BB_

I tried this expression but it gets me the full string as a result.

^\d{2}=.+_$

I'm missing something. Any help?

Upvotes: 1

Views: 43

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627507

You may use

\d{2}=.*?_(?=\d{2}=|$)

See the regex demo

You may also require no digits before the match with a (?<!\d) negative lookbehind:

(?<!\d)\d{2}=.*?_(?=\d{2}=|$)

The \d{2}=.*?_(?=\d{2}=|$) pattern matches 2 digits, =, and then any 0+ chars other than line break chars, as few as possible, up to the first _ that has two digits and = after it or is at the end of the string.

Upvotes: 3

Related Questions