2014-12-13 90 views
2

我有以下的html模式。Powershell正則表達式匹配字符串,除了第一個

href="{{url}}" class="item-name prdctNm">{{name}}</a><div> 
href="/drugs/sporanox-100-mg-33294" class="item-name prdctNm">Sporanox (100 Mg)</a> 
href="/drugs/sporan-200-mg-34240" class="item-name prdctNm">Sporan (200 Mg)</a> 
href="/drugs/spornid-500-mg-25051" class="item-name prdctNm">Spornid (500 Mg)</a> 

我想要的是讓產品的名稱,如

Sporanox (100mg), Sporan (200 mg) and Spornid (50mg).

**

更新的解決方案

**:它幾乎匹配整個頁面。從頁面上的first instance of"item-name prdctNm"last <\a> - 它匹配中間的所有內容。但是,我需要在其旁邊匹配text between "item-name prdctNm" and tag <\a>

現在,它完美的作品:

$regex = [RegEx]'"item-name prdctNm"(.[^{}<>]*)</a>' 
$url = ‘https://www.xxx.com/search/all?name=sporanox’ 
$wc = New-Object System.Net.WebClient 
$content = $wc.DownloadString($url) 
$regex.Matches($content) | ForEach-Object { $_.Groups[1].Value } 

回答

1

使用正則表達式下方,然後在最後打印組索引1,其中Groups[0]包含整個比賽和Groups[1]包含由第一組捕獲的字符。

$regex = [RegEx]'"item-name prdctNm">([^}{<>]*)</a>' 
$url = ‘https://www.xxx.com/search/all?name=sporanox’ 
$wc = New-Object System.Net.WebClient 
$content = $wc.DownloadString($url) 
$regex.Matches($content) | ForEach-Object { $_.Groups[1].Value } 
+0

非常感謝所以這個工程,如果我在RegEX中包含'<>'。有什麼辦法 - 我可以指示我的RegEX匹配到第一個。希望你能理解我的問題。 – Yogesh 2014-12-13 11:16:32

+1

簡單,在'*'...旁邊使用非貪婪或不情願的量詞'''''[RegEx]'「item-name prdctNm」>(。*?)'' – 2014-12-13 11:20:09

+0

@Yogesh把它作爲一個單獨的問題。我對PowerShell的瞭解不多。 – 2014-12-13 11:41:03