Powershell从字符串中获取所需的子字符串
我有一个字符串,看起来像这样:Powershell从字符串中获取所需的子字符串,powershell,Powershell,我有一个字符串,看起来像这样: "Comments : @{id=15161772,we are happy with the service,likes=&÷€$:#,hsbdinsjsonsjbdjdjid}" 我想得到“我们对服务很满意”的部分 我试过这样的方法: $string ="Comments : @{id=15161772,we are happy with the service,likes=&÷€$:#,hsbdinsjsonsjbdjdjid}" $str
"Comments : @{id=15161772,we are happy with the service,likes=&÷€$:#,hsbdinsjsonsjbdjdjid}"
我想得到“我们对服务很满意”的部分
我试过这样的方法:
$string ="Comments : @{id=15161772,we are happy with the service,likes=&÷€$:#,hsbdinsjsonsjbdjdjid}"
$string.split(",") | foreach {if(($_ -Notlike "*id=*" ) -and ($_ -Notlike "*likes=*")){$_ }}
我得到的结果如下:
We are happy with the service
hsbdinsjsonsjbdjdjid
我只希望得到“我们对服务很满意”。如果您可以保证您要查找的项目不包含逗号,那么您只需查看逗号分隔字符串中的第二个元素
$string.split(",")[1]
如果您可以保证您要查找的项目不包含逗号,那么您只需查看逗号分隔字符串中的第二个元素
$string.split(",")[1]
如果您可以保证您要查找的项目不包含逗号,那么您只需查看逗号分隔字符串中的第二个元素
$string.split(",")[1]
如果您可以保证您要查找的项目不包含逗号,那么您只需查看逗号分隔字符串中的第二个元素
$string.split(",")[1]
我会选择正则表达式选项
$string ='Comments : @{id=15161772,we are happy with the service,likes=&÷€$:#,hsbdinsjsonsjbdjdjid}'
$string -replace ".*?=.*?,(.*?),likes=.*", "`$1"
这将输出所需的文本,并应返回第一个等号之后的所有内容,,likes=
。如果要捕获它,只需将其分配给变量,例如:
$Comment = $string -replace ".*?=.*?,(.*?),likes=.*", "`$1"
然后,$Comment
将包含字符串我们对该服务感到满意
这应该比在逗号上拆分更灵活。有关正则表达式如何工作的示例和更多详细信息,请访问:我将选择正则表达式选项
$string ='Comments : @{id=15161772,we are happy with the service,likes=&÷€$:#,hsbdinsjsonsjbdjdjid}'
$string -replace ".*?=.*?,(.*?),likes=.*", "`$1"
这将输出所需的文本,并应返回第一个等号之后的所有内容,,likes=
。如果要捕获它,只需将其分配给变量,例如:
$Comment = $string -replace ".*?=.*?,(.*?),likes=.*", "`$1"
然后,$Comment
将包含字符串我们对该服务感到满意
这应该比在逗号上拆分更灵活。有关正则表达式如何工作的示例和更多详细信息,请访问:我将选择正则表达式选项
$string ='Comments : @{id=15161772,we are happy with the service,likes=&÷€$:#,hsbdinsjsonsjbdjdjid}'
$string -replace ".*?=.*?,(.*?),likes=.*", "`$1"
这将输出所需的文本,并应返回第一个等号之后的所有内容,,likes=
。如果要捕获它,只需将其分配给变量,例如:
$Comment = $string -replace ".*?=.*?,(.*?),likes=.*", "`$1"
然后,$Comment
将包含字符串我们对该服务感到满意
这应该比在逗号上拆分更灵活。有关正则表达式如何工作的示例和更多详细信息,请访问:我将选择正则表达式选项
$string ='Comments : @{id=15161772,we are happy with the service,likes=&÷€$:#,hsbdinsjsonsjbdjdjid}'
$string -replace ".*?=.*?,(.*?),likes=.*", "`$1"
这将输出所需的文本,并应返回第一个等号之后的所有内容,,likes=
。如果要捕获它,只需将其分配给变量,例如:
$Comment = $string -replace ".*?=.*?,(.*?),likes=.*", "`$1"
然后,$Comment
将包含字符串我们对该服务感到满意
这应该比在逗号上拆分更灵活。有关正则表达式如何工作的示例和更多详细信息,请参见:使用正则表达式拆分:
$string = 'Comments : @{id=15161772,we are happy with the service,likes=&÷€$:#,hsbdinsjsonsjbdjdjid}'
($string -split 'id=\d+,|,likes=')[1]
we are happy with the service
使用正则表达式拆分:
$string = 'Comments : @{id=15161772,we are happy with the service,likes=&÷€$:#,hsbdinsjsonsjbdjdjid}'
($string -split 'id=\d+,|,likes=')[1]
we are happy with the service
使用正则表达式拆分:
$string = 'Comments : @{id=15161772,we are happy with the service,likes=&÷€$:#,hsbdinsjsonsjbdjdjid}'
($string -split 'id=\d+,|,likes=')[1]
we are happy with the service
使用正则表达式拆分:
$string = 'Comments : @{id=15161772,we are happy with the service,likes=&÷€$:#,hsbdinsjsonsjbdjdjid}'
($string -split 'id=\d+,|,likes=')[1]
we are happy with the service