bthe0
bthe0

Reputation: 1424

regex get value

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

Answers (1)

Gilles Qu&#233;not
Gilles Qu&#233;not

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")])' -

OUTPUT:

/view?view=1

Check or saxon-lint

Upvotes: 1

Related Questions