podeig
podeig

Reputation: 2741

How to match just the first number in Regex

I have this string

<h2 id="1">1. Item 1</h2>
<h2 id="1.2">1.2. Item 1.2</h2>
<h2 id="2">2. Item 2</h2>

I need to match headers which have int numbers 1. and 2. in the text. Not 1.2.

I do it like this

<h2.*?>(.*?)[0-9]\.\s+(.*?)</h2>

It matches all headers. Where am I wrong?

Upvotes: 2

Views: 104

Answers (2)

Mayur Koshti
Mayur Koshti

Reputation: 1852

You can use the regex:

<h[1-6][^>]*>\d{1}\.(?!\d{1}\.)([^<]*)<\/h[1-6]>

Upvotes: 1

Avinash Raj
Avinash Raj

Reputation: 174696

Remove (.*?) and then add a + after [0-9] because (.*?) exists before [0-9] will match any character zero or more times, which in-turn matches 1.

<h2.*?>[0-9]+\.\s+(.*?)</h2>

DEMO

Upvotes: 1

Related Questions