Ad

PowerShell Extracting Text From A String

- 1 answer

I'm really terrible when it comes to Regex and have stuck for a little while now. How can I get Person A from the following string?

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum<br>
Affected: Lorem<br>
Duty Officer: Person A<br>
Affected: Ipsum

Thanks in advance!

Ad

Answer

You can use the below regex to achieve your results:

^Duty Officer: ([\w ]+)$

Explanation of the above regex:

^ - Represents the start of the given test String.

([\w ]+) - Represents a capturing Matching any word character([0-9A-Za-z_]) along with space character(since the names may contain space) one or more times.

$ - Represents the end of the given test string.

You can find the demo of the above regex here.

POWERSHELL COMMANDS:(You can alter the commands accordingly to suit your needs)

PS C:\Path\To\MyDesktop> $input_path='C:\Path\To\MyDesktop\test.txt'
PS C:\Path\To\MyDesktop> $output_path='C:\Path\To\MyDesktop\testResult.txt'
PS C:\Path\To\MyDesktop> $regex='^Duty Officer: ([\w ]+)$'
PS C:\Path\To\MyDesktop> select-string -Path $input_path -Pattern $regex -AllMatches | % { $_.Matches.groups[1] } | % { $_.Value } > $output_file

The above result takes the input that you provided through test.txt file and prints your desired output in testResult.txt file. Notice in the select-string command to capture only group 1 I used $_.Matches.groups[1].

For better insights of the commands used above; please refer this.

Ad
source: stackoverflow.com
Ad