0

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

1

4 Answers 4

2
<(\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).

Sign up to request clarification or add additional context in comments.

1 Comment

Should I? I don't know. But I did. By the way, regular-expressions.info is a great site to learn.
1

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>

Comments

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

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

Comments

0

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>

2 Comments

this would match <1></2> as well, tho
but this will also find <2></3> ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.