1

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

  • Starts with number 00-99 followed by an equals sign =.
  • May contain at least one character. Any character.
  • Ends with underscore '_'.

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?

1
  • If you want parts of a string then you first need to drop the anchors ^ and $ as that will always match the entire string or will not match at all. Commented Nov 30, 2018 at 13:45

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Note that you may probably want to make your pattern more verbose if there are more specific requirements. See \d{2}=(?:#[-+]?\d+\.\d+(?:_[-+]?\d+\.\d+)*)+_[a-zA-Z]{2}_ demo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.