How to extract number in line "Task (12345)" using regular expression and Powershell? I tried the following, but no chance.
$file = gc myfile.txt $matches = ([regex]"Task\(\d{1,5}\)").matches($file) # Get a list of numbers
Can someone help me find the correct regular expression?
Do you want to receive all events in the file? If so, I would do the following
$r = "^Task\((\d+)\)$" $res = gc myFile.txt | ?{ $_ -match $r } | %{ $_ -match $r | out-null ; $matches[1] }
Keep in mind that Select-String does this in a single layer:
PS> Select-String 'Task\((?<num>\d{1,5})\)' myfile.txt | %{$_.matches[0].Groups['num'].value}
If you want exactly 5 digits, you can use:
^Task\([\d{1,5}]{5}\)$
Otherwise, for an arbitrary number of digits, enter:
^Task\([\d{1,5}]+\)$