Thomas
Thomas

Reputation: 23

Regex OR stop matching at the first word

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

Upvotes: 2

Views: 81

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626826

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.

Upvotes: 2

Related Questions