Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String Powershell-如果找到两种模式,则打印该行_String_Powershell_Design Patterns - Fatal编程技术网

String Powershell-如果找到两种模式,则打印该行

String Powershell-如果找到两种模式,则打印该行,string,powershell,design-patterns,String,Powershell,Design Patterns,我有以下几行: <option title="ORACLEDB" selected="selected" value="1111">ORACLEDB</option> <option value="" selected="selected"></option> <option title="TRINITY" value="3162">TRINITY</option> 然后,它会打印出与“选项标题”或“选定”匹配的所有行,

我有以下几行:

<option title="ORACLEDB" selected="selected" value="1111">ORACLEDB</option>
<option value=""  selected="selected"></option>
<option title="TRINITY" value="3162">TRINITY</option>
然后,它会打印出与“选项标题”或“选定”匹配的所有行,而结果应该只是第一行


有什么建议吗?

您可以使用foreach浏览您的文件并搜索以下模式:

Get-Content -Path "C:\Users\am281h\Desktop\page.htm" | % { if ($_ -like "*option title*selected*"){Write-Host $_}}
%
是foreach的别名。Foreach从管道获取其引用对象

$\uu
处理foreach的当前对象

-like
允许您将对象与简单模式进行比较


*选项标题*所选*
中的
*
与x个随机字符数(包括0个字符)相等。

您可以使用foreach浏览文件并搜索如下模式:

Get-Content -Path "C:\Users\am281h\Desktop\page.htm" | % { if ($_ -like "*option title*selected*"){Write-Host $_}}
%
是foreach的别名。Foreach从管道获取其引用对象

$\uu
处理foreach的当前对象

-like
允许您将对象与简单模式进行比较


*选项标题*所选*
中的
*
与x个随机字符数(包括0个字符)相等。

这看起来像xml节点=>我会将行作为xml部分处理:

$src = '<option title="ORACLEDB" selected="selected" value="1111">ORACLEDB</option>',
'<option value=""  selected="selected"></option>',
'<option title="TRINITY" value="3162">TRINITY</option>'

$src | 
  % {[xml]$_} | #its xml!
  ? { $_.option.title -and $_.option.selected } | #filter
  % { write-host $_.OuterXml } #print as text
$src='ORACLEDB',
'',
“三位一体”
$src|
%{[xml]$}|#它的xml!
? {$\.option.title-和$\.option.selected}|#过滤器
%{write host$\.OuterXml}#以文本形式打印

这看起来像xml节点=>我会将行作为xml部分处理:

$src = '<option title="ORACLEDB" selected="selected" value="1111">ORACLEDB</option>',
'<option value=""  selected="selected"></option>',
'<option title="TRINITY" value="3162">TRINITY</option>'

$src | 
  % {[xml]$_} | #its xml!
  ? { $_.option.title -and $_.option.selected } | #filter
  % { write-host $_.OuterXml } #print as text
$src='ORACLEDB',
'',
“三位一体”
$src|
%{[xml]$}|#它的xml!
? {$\.option.title-和$\.option.selected}|#过滤器
%{write host$\.OuterXml}#以文本形式打印

Select String-Pattern
是基于正则表达式的,所以使用一个
Select String-Pattern”选项标题。+selected“
正则表达式中的点匹配任何字符,量词
+
表示任何数字,但至少有一个。
Select String-Pattern
是基于正则表达式的,所以使用一个
Select String-Pattern“option title.+selected”
正则表达式中的点与任何字符匹配,量词
+
表示任何数字,但至少一个。