0

I need to get multiple matches between multiple possible pattern

This is raw data example:

DC00-01-14 blabla blabla MB00-07-10 blublu CN03 bli BLI2454 bli bli CN02 bloblo bloblo bloblo SYSA bloublou bloublou bloublou CN06 blaiblai blaiblai blaiblai METR blybly blybly blybly ppag blubliblouBFD 454

and the regex should match like this:

DC00-01-14 blabla blabla
MB00-07-10 blublu
CN03 bli BLI2454 bli bli
CN02 bloblo bloblo bloblo
SYSA bloublou bloublou bloublou
CN06 blaiblai blaiblai blaiblai
METR blybly blybly blybly
ppag blubliblouBFD 454

With this expression, I am able to detect the keys:

((DC\d{2}[-]\d{2}[-]\d{2})|(MB\d{2}[-]\d{2}[-]\d{2})|(CN0\d{1})|(SYSA)|(ppag)|(METR))

but I need to get the string in between with the first key (without the second key) like in my result example.

What should I do?

https://regex101.com/r/vyi864/1

1
  • 1
    When asking about regular expressions, it is a good idea to specify which host language you're working with. The answer can vary — there are lots of different species of regular expression, and what works in Perl or Python may not work in Java or JavaScript. Commented Jul 31, 2017 at 20:40

1 Answer 1

2

What I did is placed your regexp in the beggining so it will match once and used a pattern similar to(?:(?!REGEXP).)* which would match till the REGEXP is found but does not include the regexp. put your regexp for the token in place of REGEXP.

((DC\d{2}[-]\d{2}[-]\d{2})|(MB\d{2}[-]\d{2}[-]\d{2})|(CN0\d{1})|(SYSA)|(ppag)|(METR))(?:(?!(((DC\d{2}[-]\d{2}[-]\d{2})|(MB\d{2}[-]\d{2}[-]\d{2})|(CN0\d{1})|(SYSA)|(ppag)|(METR)))).)*

For Ignoring New Line, Try something like this (?:\s*(?!REGEXP).)* instead of (?:(?!REGEXP).)* . The \s* would match newline if present.

(((DC|MB)\d{2}[-]\d{2}[-]\d{2})|(CN0\d{1})|(SYSA)|(ppag)|(METR))(?:\s*(?!((((DC|MB)\d{2}[-]\d{2}[-]\d{2})|(CN0\d{1})|(SYSA)|(ppag)|(METR)))).)*

Hope this will help.

You can see in the right hand that the full match is in the desired way. enter image description here

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

3 Comments

this is great seems to be perfect but now if i don t want to stop at new line ?
If you're using Java, you can use DOTALL and MULTILINE flags when compiling a regular expression. You can find similar flags in other languages too.
@byron I have added the RegExp which does not stop at new line. Hope it solves your problem. regex101.com/r/vyi864/3

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.