DaveNOTDavid
DaveNOTDavid

Reputation: 1803

Regex pattern for all chars and white space chars up until a double line break

With the following text:

** First Header
------------------------------------------------------------

(https://www.stackoverflow.com) Stack Overflow

Description: Stack Overflow

Google (https://www.google.com)

Description: Google


** Second Header

... how can I use a regex pattern that matches up until the second header with a double line break, so an output like:

** First Header
------------------------------------------------------------

(https://www.stackoverflow.com) Stack Overflow

Description: Stack Overflow

Google (https://www.google.com)

Description: Google

With the following regex (matching the first header with all characters as well as white space characters, hence the usages of \s and \S with the succeeding regex literals, and a negative lookahead of a double line break), I feel like I'm definitely on the right track:

RegExp("\\*\\* First Header[\\s\\S]*?(\\r?\\n(?!\\r?\\n))")

... but only outputs:

** First Header

Upvotes: 1

Views: 98

Answers (3)

oleedd
oleedd

Reputation: 446

The next regex will work:

/\*[^]+?(?=\s+\*)/

or

RegExp("\\*[^]+?(?=\\s+\\*)")

And it is the shortest way.

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626851

You may use

/(\*\* First Header[\s\S]*?)(?:\r?\n){3}/

Or a JS regex only compatible

/(\*\* First Header[^]*?)(?:\r?\n){3}/

See the regex demo

Details

  • (\*\* First Header[\s\S]*?) - Group 1 capturing:
    • \*\* First Header - a literal ** First Header substring
    • [\s\S]*? - any 0+ chars, as few as possible
  • (?:\r?\n){3} - three occurrences of an optional CR followed with LF chars (so, matching two blank lines in effect).

Upvotes: 2

Kenneth Obando
Kenneth Obando

Reputation: 83

A solution could be:

RegEx("(\\*\\*[\\s\\S]*)(\\n\\n\\*\\*)")

extracting the first group.

You can see the example in https://regex101.com/r/WPN83b/1

Upvotes: 0

Related Questions