Bruce Dou
Bruce Dou

Reputation: 4761

How to find this regex using PHP

find all the <1></1> and <2></2> and <3></3>... in a string.

Upvotes: 0

Views: 85

Answers (4)

BitDrink
BitDrink

Reputation: 1193

You can use the following regex: <[0-9]></[0-9]>

EDIT: To avoid that the search matches <2></3> too, you can use a sub-expression and a backreference to instantiate it: <([0-9])></\1>

Upvotes: 0

Joey
Joey

Reputation: 354466

<(\d+)></\1>

should work. This ensures that the regex won't match <1></4> for example.

\1 is here a backreference which must match exactly the same string as the first capturing group (the (\d+) in the first angle brackets).

Upvotes: 2

Sarfraz
Sarfraz

Reputation: 382696

<STYLE[^>]*>([\s\S]*?)<\/STYLE[^>]*>

Just replace STYLE with your tag like 1, 2 whatever.

Upvotes: 0

David Hedlund
David Hedlund

Reputation: 129792

One regex to match any of them?

<([1-3])></\1>

Should there code allow for anything to be posted in between the > and the <? Something like this then:

<([1-3])>(.*?)</\1>

Upvotes: 1

Related Questions