Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

107
Views
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

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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.
about 4 years ago · Juan Pablo Isaza Report

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!