我有以下 html 模式。
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).
**
Updated Solution
** :它几乎匹配整个页面。从 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,其中 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 }
关于html - Powershell正则表达式匹配除第一个以外的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27457951/