Dmitry Bubnenkov
Dmitry Bubnenkov

Reputation: 9869

How to select parts of text from markdown with regexp?

I have next text:

#Header
my header text

##SubHeader
my sub header text

###Sub3Header
my sub 3 text

#Header2
my header2 text

I need to select text from "#Header" to "#Header2".

I tried to wrote regexp: http://regexr.com/3ffva but it's do not match what i needed.

Upvotes: 1

Views: 1065

Answers (3)

builder-7000
builder-7000

Reputation: 7637

Depending on your regex flavor you can use:

(^#{1}.+)(.*\n)*

As shown here: http://regexr.com/3fg08

Alternately, you can use Vim's very magic mode:

\v(^#{1}.+)(.*\n)*(^#{1}\w+)

Upvotes: 0

MohaMad
MohaMad

Reputation: 2855

With looking ahead for closing capture and also matching, before next heading:

1- without multi-line flag

(^|\n)#([^#]+?)\n([^]+?)(?=\n#[^#]|$)

Demo without multi-line flag

Description:

  • Group 1 captures first of string or new line that follows # and no other #, that means new Heading starts there.

  • Group 2 captures Heading title

  • Group 3 captures any thing till the next heading or end of string

  • Group 4 is non-capturing and looks ahead for new heading, or end of text.


2- with multi-line flag

^#([^#]+?)\n([^]+?)(?=^#[^#])

Demo with Multi-line flag

Description:

  • first, add #-- at the end of text, for matching last Heading by this regex!

  • Starts matching from first char of line by ^ and matches # with no # in heading text. Group 1 captured: Heading before \n

  • Group 2 captures texts till next Heading start, that defined by just one # at starting line.

Upvotes: 1

Whothehellisthat
Whothehellisthat

Reputation: 2152

^#[^#\n]+([\W\w]*?)^#[^#\n]+

Basic idea: find first level-1 heading, find any text until... second level-1 heading.

  • ^#[^#\n]+ first level-1 heading
    • ^ start of line (because of multi-line flag)
    • [^#\n]+ Any character that isn't # or a newline character. Repeat 1 or more times.
  • ([\W\w]*?) any text until next matching part
  • ^#[^#\n]+ second level-1 heading (see above)

Flags: multiline.

Upvotes: 2

Related Questions