rgshenoy
rgshenoy

Reputation: 449

Regex to match a section in a piece of text

I've been pondering over this for hours now and I've not come up with a working regular expression :(

I have paragraphs of text, some of the lines are section headings and these are what I want to match.

Each section begins with a number, the number could be 1 or 1.1 or 1.1.1 or 1.1.1.1 etc. And after one or more whitespaces or tabs the section title begins and ends with a CR or LF.

For example

3.4.1 Section about how regular expressions are built.

I need to match string like the one above in a body of plain text.

Any pointers would be highly appreciated. :) Thx!

Upvotes: 1

Views: 2678

Answers (2)

Himanshu
Himanshu

Reputation: 2454

A few digits followed by a few groups of dot preceded few digits

\d+(\.\d+)+

Upvotes: 2

Qtax
Qtax

Reputation: 33908

You could use an expression like this to match such lines:

(?m)^\d+(?:\.\d+)*[ \t]+\S.*$

Explained and tested at regex101.com

Upvotes: 1

Related Questions