0

Ey stackoverflow, I got this href:

<a href="/view?view=1"></a>

I am looking for a regexp that gets the first href content where href starts with /view. How am I supposed to do it ? Been looking everywhere.

bthe0
  • 1,274
  • 2
  • 13
  • 24

1 Answers1

1

As stated by others in comments, don't use regex to parse HTML, use a proper parser. Check: RegEx match open tags except XHTML self-contained tags

$ echo '<a href="/view?view=1"></a>' |
    xmlstarlet sel -t -v '//a/@href[starts-with(., "/view")]' -

or

$ echo '<a href="/view?view=1"></a>' |
    saxon-lint --xpath 'string(//a/@href[starts-with(., "/view")])' -

OUTPUT:

/view?view=1

Check or saxon-lint

Community
  • 1
  • 1
Gilles Quenot
  • 154,891
  • 35
  • 213
  • 206