PowerShell Extracting Text From A String
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!
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.
Related Questions
- → How to replace *( in a string
- → Regex extract string after the second "." dot character at the end of a string
- → CodeMirror regex doesn't match behavior typical Javascript regex parser
- → JS Get multiple links in a string
- → Using capture group $1 to access object's key value
- → Difference between /(a|b)/ and /[ab]/ in JavaScript split() using regex
- → In Nginx, how to match and entire URL and Query String and redirect to a URL and Query String
- → Reorder lines in a string based on first two numbers inside
- → How to manage URL with or without ? and /
- → Not sure why this Regex is returning true
- → Laravel extract excerpt from content using tinymce
- → Cannot get my multiple regex working for specific case in URL structure
- → laravel5.1 validate number