在PowerShell中若想要使用regular expression
可使用 -match

例如
若想要解析此段html
取出<html>中的資料
可使用以下指令

"<html><a>abc</a></html>" -match '<(\w+)>(.+)</\1>'

\1
代表前面\w+所match出的字串
因此整個match字串則視為'<html>(.+)</html>'

本例因為有match到因此會回傳True

我們可使用$matches可看出match的結果

$matches[0]代表整個match字串
$matches[1]代表(\w+)所match到的字串html
$matches[2]代表(\.+)所match到的字串<a>abc</a>

以此法可遞迴下去解析$matches[2]的字串
直到$matches[2]無法match出為止

arrow
arrow
    全站熱搜

    ayowu 發表在 痞客邦 留言(0) 人氣()