-1

I have regular expression:

(?<message>.*)[\.](?:[\s]id:(?<id>.*)[\.])? code:(?<code>.*)[\.]

I have 2 cases for input text:

1. Failed to send sms. code:5063.
2. Failed to send sms. id:23. code:5063.

For the 1st case I want to have output like this:

1. Group message: Failed to send sms
2. Group code: 5063

For the 2nd case I want to have output like this:

1. Group message: Failed to send sms
2. Group id: 23
3. Group code: 5063

For the 1st case it's ok, but for the 2nd case I have:

1. Group Message: Failed to send sms. id:23
2. Group Code: 5063

It concatenates message and id. I can't understand why this is happening.

You can check it in the online editor here: https://regex101.com/r/bOWjKY/1/

Thank you in advance!

user3783243
  • 5,368
  • 5
  • 22
  • 41
Phil
  • 82
  • 8

1 Answers1

0

The * on the message part of regex is greedy. Add a ? to make it stop at the first occurance.

(?<message>.*?)[\.](?:[\s]id:(?<id>.*)[\.])? code:(?<code>.*)[\.]
                 ^

https://regex101.com/r/bOWjKY/2/

alternatively you could use U modifier which inverts (ungreedy) behavior.

(?U)(?<Message>.*)[\.](?:[\s]id:(?<id>.*)[\.])? Code:(?<Code>.*)[\.]
user3783243
  • 5,368
  • 5
  • 22
  • 41
  • Thanks a lot! It's really help me. I spent ton of hours to understand. You are my angel of mercy! – Phil Dec 04 '20 at 18:45