Home > Mobile >  Regex OR stop matching at the first word
Regex OR stop matching at the first word

Time:08-31

Here are the data I receive :

I want to get only the message of the question. It is possible that my question is on several lines. For that I put "/s" at the end of my regex.

My regex is : /question : ((.|)*)\n?(line_1|line_2)/s

Example 1:

question : this is my question
line_1: 0
line_2: 1

Example 2 :

question : this is my question
line_2: 1

For my example 2 it's ok it works but for example 1 the condition "or" does not stop at the first occurrence found, do you have a solution ? Thanks for your help

CodePudding user response:

You can use

question\s*:\s*(\S.*(?:\r?\n. )*)

See the regex demo. Note I added \r? because the . in JavaScript does not match carriage returns.

If line_1 and line_2 are line_ digits and must be present, then include them as

question\s*:\s*(\S.*(?:\r?\nline_\d.*)*)

See this regex demo.

Details:

  • question - a word
  • \s*:\s* - a colon enclosed with zero or more whitespaces
  • (\S.*(?:\r?\nline_\d.*)*) - Group 1:
    • \S - a non-whitespace char
    • .* - the rest of the line
    • (?:\r?\nline_\d.*)* - zero or more lines that start with line_ and a digit and then the rest of the line can have any chars. (?:\r?\n. )* matches any zero or more non-empty (zero-length) lines.

CodePudding user response:

Thanks for the answer and sorry I didn't explain it well.

My line_1 and line_2 are 2 different words and I would like them to be excluded from my group 1. Another condition the message can be on several lines.

I put you a better example :

question: **This message can be on several lines.
Continuation of the message
end of the message**
currency_mortgage : 0
property_alert: 1

Sometimes the "currency_mortgage" is not there, so we have :

question: **This message can be on several lines.
Continuation of the message
end of the message**
property_alert: 1

I don't know how to stop the capture of group 1 before either "property_alert" or "currency_mortgage" to really take only the message content.

  • Related