2

I have this regex

(?<=TG00).*?(?=#)

which extracts all strings between TG00 and #. Demo: https://regex101.com/r/04oqua/1

Now, from above results I want to extract only the string which contains TG40 155963. How can I do it?

1
  • have any preferences to programming languages? Commented Jan 18, 2018 at 11:50

3 Answers 3

2

Try this pattern:

TG00[^#]*TG40 155963[^#]*#

This pattern just says to find the string TG40 155963 in between TG00 and an ending #. For the sample data in your demo there were 3 matches.

Demo

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

1 Comment

Many thanks to all for the answers, all are working great! In my special needs this seems to work smoother. Ty!
1

For some reason appending .*? to your lookbehind results in engine error, but works fine with lookahead. Regex below does not match your text exactly, but it does extract it via capture group.

(?<=TG00).*?(TG40 155963)(?=.*?#)

Comments

1

You can use this regex with a lookahead and negated character class:

(?<=TG00)(?=[^#]*TG40 155963)[^#]+(?=#)

RegEx Demo

RegEx Explanation:

  • (?<=TG00): Assert that we have TG00 at previous position
  • (?=[^#]*TG40 155963): Lookahead to assert we have string TG40 155963 after 0 or more non-# characters, ahead
  • [^#]+: Match 1+ non-# characters

2 Comments

You seem to have dropped the (?=#) requirement from the end. I guess it should not match if there isn't actually a # after the match.
Thanks @tripleee, added it now

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.