2

My source is something like:

... <a href='...'>...</a> .... <a href='...'>....</a>  ...  <a href='...'>... </a> ...

<a href='(.*?)'> will give me the first href value? (For readability I omit \ for those that need escaping but I hope you know what I mean.) Is there a way to return a collection of all that match the pattern?

Old Geezer
  • 14,854
  • 31
  • 111
  • 198
  • 2
    http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – A. Webb Aug 22 '13 at 13:43
  • Thanks, but I am not really trying to parse general Html text. My source string is a very well-formatted Html, so I don't have to deal with all the countless possibilities of Html syntax. My question is more on whether there is a Regex syntax or command to return a collection of all (I don't know how many up front) that matches a given pattern. – Old Geezer Aug 22 '13 at 13:51
  • 2
    @A.Webb As funny as that link is, it really doesn't provide any meaningful information. Geezer, have you seen [this question](http://stackoverflow.com/questions/3141851/how-to-capture-multiple-regex-matches-from-a-single-line-into-the-matches-mag)? I'm not sure if you're looking for more help with what expression to write, or how to use powershell's regex functionality? – FrankieTheKneeMan Aug 22 '13 at 14:14
  • @FrankieTheKneeMan Yes, my question is the same as the one in the link. Using SLS achieves what I want to do. Thanks. – Old Geezer Aug 22 '13 at 14:40

1 Answers1

4
$s = "tralala <a href='A'>A</a> blah <a href='BB'>BB</a>  blih <a href='CCC'>CCC</a>"
$matches = ($s | select-string "\<a.+?href.*?=.*?\'.*?'\>.*?\</a\>" -allmatches).matches
$matches | % { $_.Value }
David Brabant
  • 41,623
  • 16
  • 83
  • 111