选择带有转义字符的字符串
Selection for strings with escaping characters
除了某些包含 \
和另一个问题的行之外,以下代码以某种方式完成了我想要的操作。我想首先解决有关包含 \
.
的字符串的有缺陷选择的不便之处
$FC=(gc E3.txt)
$L=$FC.length
$SERI=foreach($k in 0..($L-1)) {$FC[$k]}
$list='E4.txt' # initially, no content
foreach($x in $SERI){sls -path E0.txt -pattern $x -CaseSensitive |ac $list}
$SCH=(gc $list).Replace('E0.txt:','')
clc $list
ac $list $SCH
下面是3个文件的内容。
E3 (input 2) E0 (input 1) E4 (output)
_ahg _ab \> croitre\ 2:_cnur \> plein\
_cnur _cnur \> plein\ 4:_cnv \> pratique\
_cnv _cho \> cartouche\ 7:_cob \> plaque\
_co\m u _cnv \> pratique\ 9:_coe \> étang (\!\!: stn: taief)\
_co\upp m _co\m u \> lequel\
_cob _co\upp m \> des morceaux\
_coe _cob \> plaque\
_drj \> complet\
_coe \> étang (\!\!: stn: taief)\
_coi \> prairie\
我希望选择诸如 _co\m u \> lequel\
之类的行,但它们会导致出现错误语句,如下所示。
sls : La chaîne _co\m u n’est pas une expression régulière valide : analyse de "_co\m u " - Séquence d'échappement \m non reconnue.
Au caractère Ligne:7 : 20
+ ... ($x in $lt){sls -path E0.txt -pattern $x - ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument : (:) [Select-String], ArgumentException
+ FullyQualifiedErrorId : InvalidRegex,Microsoft.PowerShell.Commands.SelectStringCommand
是否可以对该代码进行修改以使其工作?
E3.txt
文件包含正则表达式元字符。 保留以下字符:[]().\^$|?*+{}
(参见About Regular Expressions)。使用
任一
Select-String
中的-SimpleMatch
参数如下(它防止将Pattern
参数的值解释为正则表达式语句):
foreach( $x in $SERI ) {
Select-String -Path E0.txt -Pattern $x -SimpleMatch -CaseSensitive |
Add-Content $list
}
或
[regex]::Escape()
静态方法如下:
$SERI = foreach ($k in 1..$($FC.Count -1)) { [regex]::Escape( $FC[$k] ) }
有关解释,请阅读 (.NET
) Regex Class 文章:
Escapes a minimal set of characters (\, *, +, ?, |, {, [, (,), ^, $,
., #, and white space) by replacing them with their escape codes. This
instructs the regular expression engine to interpret these characters
literally rather than as metacharacters.
例如(参见E3.txt
文件中的第5行):[regex]::Escape('_co\upp m')
returns _co\upp\ m
.
除了某些包含 \
和另一个问题的行之外,以下代码以某种方式完成了我想要的操作。我想首先解决有关包含 \
.
$FC=(gc E3.txt)
$L=$FC.length
$SERI=foreach($k in 0..($L-1)) {$FC[$k]}
$list='E4.txt' # initially, no content
foreach($x in $SERI){sls -path E0.txt -pattern $x -CaseSensitive |ac $list}
$SCH=(gc $list).Replace('E0.txt:','')
clc $list
ac $list $SCH
下面是3个文件的内容。
E3 (input 2) E0 (input 1) E4 (output)
_ahg _ab \> croitre\ 2:_cnur \> plein\
_cnur _cnur \> plein\ 4:_cnv \> pratique\
_cnv _cho \> cartouche\ 7:_cob \> plaque\
_co\m u _cnv \> pratique\ 9:_coe \> étang (\!\!: stn: taief)\
_co\upp m _co\m u \> lequel\
_cob _co\upp m \> des morceaux\
_coe _cob \> plaque\
_drj \> complet\
_coe \> étang (\!\!: stn: taief)\
_coi \> prairie\
我希望选择诸如 _co\m u \> lequel\
之类的行,但它们会导致出现错误语句,如下所示。
sls : La chaîne _co\m u n’est pas une expression régulière valide : analyse de "_co\m u " - Séquence d'échappement \m non reconnue.
Au caractère Ligne:7 : 20
+ ... ($x in $lt){sls -path E0.txt -pattern $x - ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument : (:) [Select-String], ArgumentException
+ FullyQualifiedErrorId : InvalidRegex,Microsoft.PowerShell.Commands.SelectStringCommand
是否可以对该代码进行修改以使其工作?
E3.txt
文件包含正则表达式元字符。 保留以下字符:[]().\^$|?*+{}
(参见About Regular Expressions)。使用
任一
Select-String
中的-SimpleMatch
参数如下(它防止将Pattern
参数的值解释为正则表达式语句):
foreach( $x in $SERI ) {
Select-String -Path E0.txt -Pattern $x -SimpleMatch -CaseSensitive |
Add-Content $list
}
或
[regex]::Escape()
静态方法如下:
$SERI = foreach ($k in 1..$($FC.Count -1)) { [regex]::Escape( $FC[$k] ) }
有关解释,请阅读 (.NET
) Regex Class 文章:
Escapes a minimal set of characters (\, *, +, ?, |, {, [, (,), ^, $, ., #, and white space) by replacing them with their escape codes. This instructs the regular expression engine to interpret these characters literally rather than as metacharacters.
例如(参见E3.txt
文件中的第5行):[regex]::Escape('_co\upp m')
returns _co\upp\ m
.