1

I'm trying to extract a certain part of the text from a file. I'm having trouble making the regular expression match the least number of characters as possible.

Here is an example text file.

UNIQUE
sdkjbskdfb....
UNIQUE
lnasdljnkjn......
UNIQUE
*Text from here is needed*
UNIQUE2
*Text from here is needed*
UNIQUE

My best effort was this. "UNIQUE(.\*?)UNIQUE2(.\*?)UNIQUE"

Unfortunately this matches the whole thing because it uses the first UNIQUE value instead of the third one.

1 Answer 1

1

You need a negative lookahead:

UNIQUE((?:(?!UNIQUE).)*?)UNIQUE2(.*?)UNIQUE

Regular expression visualization

Debuggex Demo

This says, find UNIQUE followed by some string that doesn't contain UNIQUE again before you hit UNIQUE2, etc.

Let me know if you need clarification.

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

Comments

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.