用于解析字符串的 Powershell 脚本
Powershell script to parse a string
我有一个像
这样的字符串
设置 Cookie:
ehCookie="X0xhc3RFbnRyeVVSTENvbnRleHQ9aHR0cHM6Ly93d3c5LmNtLmVoZWFsdGhpbnN1cmFuY2UuY29tL2VoaS9ORVdCT0xvZ2luLmRzfF9MYXN0VVJMPWh0dHBzOi8vd3d3OS5jbS5laGVhbHRoaW5zdXJhbmNlLmNvbS9laGkvRGlzcGF0Y2guZnN8X0xhc3RXZWJDb250ZXh0PUJPfF9TZXNzaW9uU3RhcnQ9MDUtMjItMjAyMSAwMjoxMTo0M3xfV2ViQ29udGV4dD1CTw=="; Version=1; Path=/; Secure; HttpOnly,bov1-route=1621674704.476.8922.899787; Path=/; Secure; HttpOnly,JSESSIONID=304447EB52E6D43AB4ABA1191D92D07A; Path=/; Secure; HttpOnly
我想像
一样解析ehCookie
&JSESSIONID
的值
X0xhc3RFbnRyeVVSTENvbnRleHQ9aHR0cHM6Ly93d3c5LmNtLmVoZWFsdGhpbnN1cmFuY2UuY29tL2VoaS9ORVdCT0xvZ2luLmRzfF9MYXN0VVJMPWh0dHBzOi8vd3d3OS5jbS5laGVhbHRoaW5zdXJhbmNlLmNvbS9laGkvRGlzcGF0Y2guZnN8X0xhc3RXZWJDb250ZXh0PUJPfF9TZXNzaW9uU3RhcnQ9MDUtMjItMjAyMSAwMjoxMTo0M3xfV2ViQ29udGV4dD1CTw==
和
304447EB52E6D43AB4ABA1191D92D07A
我该如何为此编写 powershell 脚本。任何帮助将不胜感激。
你可以这样做($str
包含要解析的字符串):
if ($str -match 'ehCookie=([^;]+).*JSESSIONID=([\dA-F]+)') {
$ehCookie = $matches[1]
$jSessionId = $matches[2]
}
或
$ehCookie = ([regex]'(?i)ehCookie=([^;]+)').Match($str).Groups[1].Value
$jSessionId = ([regex]'(?i)JSESSIONID=([\dA-F]+)').Match($str).Groups[1].Value
正则表达式详细信息:
ehCookie= Match the characters “ehCookie=” literally
( Match the regular expression below and capture its match into backreference number 1
[^;] Match any character that is NOT a “;”
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
. Match any single character that is not a line break character
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
JSESSIONID= Match the characters “JSESSIONID=” literally
( Match the regular expression below and capture its match into backreference number 2
[\dA-F] Match a single character present in the list below
A single digit 0..9
A character in the range between “A” and “F”
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
在第二个示例中,(?i)
使 .Match
不区分大小写,这在使用第一个示例中使用的 -match
运算符时不需要示例
我有一个像
这样的字符串设置 Cookie:
ehCookie="X0xhc3RFbnRyeVVSTENvbnRleHQ9aHR0cHM6Ly93d3c5LmNtLmVoZWFsdGhpbnN1cmFuY2UuY29tL2VoaS9ORVdCT0xvZ2luLmRzfF9MYXN0VVJMPWh0dHBzOi8vd3d3OS5jbS5laGVhbHRoaW5zdXJhbmNlLmNvbS9laGkvRGlzcGF0Y2guZnN8X0xhc3RXZWJDb250ZXh0PUJPfF9TZXNzaW9uU3RhcnQ9MDUtMjItMjAyMSAwMjoxMTo0M3xfV2ViQ29udGV4dD1CTw=="; Version=1; Path=/; Secure; HttpOnly,bov1-route=1621674704.476.8922.899787; Path=/; Secure; HttpOnly,JSESSIONID=304447EB52E6D43AB4ABA1191D92D07A; Path=/; Secure; HttpOnly
我想像
一样解析ehCookie
&JSESSIONID
的值
X0xhc3RFbnRyeVVSTENvbnRleHQ9aHR0cHM6Ly93d3c5LmNtLmVoZWFsdGhpbnN1cmFuY2UuY29tL2VoaS9ORVdCT0xvZ2luLmRzfF9MYXN0VVJMPWh0dHBzOi8vd3d3OS5jbS5laGVhbHRoaW5zdXJhbmNlLmNvbS9laGkvRGlzcGF0Y2guZnN8X0xhc3RXZWJDb250ZXh0PUJPfF9TZXNzaW9uU3RhcnQ9MDUtMjItMjAyMSAwMjoxMTo0M3xfV2ViQ29udGV4dD1CTw==
和
304447EB52E6D43AB4ABA1191D92D07A
我该如何为此编写 powershell 脚本。任何帮助将不胜感激。
你可以这样做($str
包含要解析的字符串):
if ($str -match 'ehCookie=([^;]+).*JSESSIONID=([\dA-F]+)') {
$ehCookie = $matches[1]
$jSessionId = $matches[2]
}
或
$ehCookie = ([regex]'(?i)ehCookie=([^;]+)').Match($str).Groups[1].Value
$jSessionId = ([regex]'(?i)JSESSIONID=([\dA-F]+)').Match($str).Groups[1].Value
正则表达式详细信息:
ehCookie= Match the characters “ehCookie=” literally
( Match the regular expression below and capture its match into backreference number 1
[^;] Match any character that is NOT a “;”
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
. Match any single character that is not a line break character
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
JSESSIONID= Match the characters “JSESSIONID=” literally
( Match the regular expression below and capture its match into backreference number 2
[\dA-F] Match a single character present in the list below
A single digit 0..9
A character in the range between “A” and “F”
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
在第二个示例中,(?i)
使 .Match
不区分大小写,这在使用第一个示例中使用的 -match
运算符时不需要示例