如何使用正则表达式和Powershell提取字符串"Task(12345)"中的数字?

Mar*_*tin 4 regex powershell

如何使用正则表达式和Powershell提取字符串"Task(12345)"中的数字?我尝试了以下,但没有机会.

$file = gc myfile.txt
$matches = ([regex]"Task\(\d{1,5}\)").matches($file)
# Get a list of numbers
Run Code Online (Sandbox Code Playgroud)

有人可以帮我找到正确的正则表达式吗?

小智 7

请记住,Select-String使这个单行:

PS> Select-String 'Task\((?<num>\d{1,5})\)' myfile.txt | 
        %{$_.matches[0].Groups['num'].value}
Run Code Online (Sandbox Code Playgroud)


Jar*_*Par 5

你想在文件中得到所有的出现吗?如果是这样,我会做以下事情

$r = "^Task\((\d+)\)$"
$res = gc myFile.txt | 
  ?{ $_ -match $r } |
  %{ $_ -match $r | out-null ; $matches[1] }
Run Code Online (Sandbox Code Playgroud)