find all the <1></1> and <2></2> and <3></3>... in a string.
Asked
Active
Viewed 80 times
0
-
Before you go too far down this road, please read this question: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Greg Hewgill Dec 08 '09 at 10:18
4 Answers
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).
Joey
- 330,812
- 81
- 665
- 668
-
2Should I? I don't know. But I did. By the way, http://www.regular-expressions.info/ is a great site to learn. – Joey Dec 08 '09 at 10:18
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>
David Hedlund
- 125,403
- 30
- 199
- 217
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>
BitDrink
- 1,185
- 1
- 18
- 24
0
<STYLE[^>]*>([\s\S]*?)<\/STYLE[^>]*>
Just replace STYLE with your tag like 1, 2 whatever.
Sarfraz
- 367,681
- 72
- 526
- 573