Ask Your Question
4

How can the regex be applied to match from a certain pattern until either an empty line or the occurrence of the next pattern?

asked 2022-04-19 11:00:00 +0000

qstack gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2022-12-16 19:00:00 +0000

scrum gravatar image

You can use the following regex to match from a certain pattern until either an empty line or the occurrence of the next pattern:

pattern[\s\S]*?(?=\n\s*\n|\n\s*pattern|$)

Here, "pattern" refers to the specific pattern you want to match.

Explanation: - [\s\S]*? matches any character (including line breaks) zero or more times lazily (i.e., the shortest possible match). - (?=\n\s*\n|\n\s*pattern|$) is a positive lookahead that checks for either an empty line (\n\s*\n), the occurrence of the next pattern (\n\s*pattern), or the end of the string ($).

Once you apply this regex, you should get a match that includes the entire block of text starting from the pattern and ending at the empty line or next occurrence of the pattern.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-04-19 11:00:00 +0000

Seen: 11 times

Last updated: Dec 16 '22