Reputation: 1424
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.
Upvotes: 0
Views: 33
Reputation: 185005
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")])' -
/view?view=1
Upvotes: 1