use*_*859 2 regex windows cmd findstr
我正在尝试使用Windows命令提取子字符串。
我想提取一个看起来像这样的数字:1.2.3.4或更确切地说[anyPosInteger.anyPosInteger.anyPosInteger.anyPosInteger]。
我以为我正在使用正则表达式。
这是代码:
set mystring="whatever 1.2.3.4 whatever talk to the hand"
echo %mystring% | findstr /r "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+" >foundstring
echo %foundstring%
Run Code Online (Sandbox Code Playgroud)
如果“ foundstring”为“ 1.2.3.4”,那将是一个很好的选择。
findstr似乎仅在找到与正则表达式匹配的情况下才返回整个字符串。更有趣的是,我所构建的正则表达式似乎不被findstr喜欢。
这适用于“ [0-9]。[0-9]。[0-9]。[0-9]”,但仍返回整个字符串。
我在这里做错了什么?:)
/H
不幸的findstr是,不能用于提取匹配项,并且findstr不支持+作为量词,您必须使用:
findstr /R "[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*"
Run Code Online (Sandbox Code Playgroud)
但它只会返回整行,仅用于提取正则表达式匹配项,我建议您使用powershell
"whatever 1.2.3.4 whatever talk to the hand" | Select-String -Pattern '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | % { $_.Matches } | % { $_.Value }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2239 次 |
| 最近记录: |