1

I want to match words inside angle brackets (html tags):

<MatchedWord></MartchedWord>

This is what I have so far:

/\v\<\w+\>

The problem is that it matches the <> too and the /.

How to do it so it only matches the word?

alexchenco
  • 53,565
  • 76
  • 241
  • 413

3 Answers3

5

You can assert matching before and after text without including that in the match via Vim's special \zs (match start) and \ze (match end) atoms:

/<\/\?\zs\w\+\ze\/\?>

I've included an optional (\?) slash on both side (e.g. </this> and <this/>. Also note that \w\+ isn't a completely correct expression for XML or HTML tags (but it can be a good-enough approximation, depending on your data).

Alternative

For most other regular expression engines, you need to use lookbehind and lookahead to achieve this. Vim has those, too (\@<= and \@=), but the syntax is more awkward, and the matching performance may be poorer.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
2

You dont need to escape angle brackets (square brackets are []) since they are not special characters. You can use capturing groups

<\/?(.+)>
Jeanno
  • 2,769
  • 4
  • 23
  • 31
1

In a non-vim environment, this is achieved using positive lookbehind and lookahead as such:

/(?<=<).*?(?=>)/

This matches the following:

<test>         // test
</content>     // /content
<div id="box"> // div id="box"
<div id="lt>"> // div id="lt

So as you can see by the final example it's not perfect, but you are using regex on html so you get what you pay for

See the regex in action

Community
  • 1
  • 1
Devon Parsons
  • 1,234
  • 14
  • 23