Powershell - Extract strings before and after specific substring

188 views Asked by At

I am trying to find a way to extract all text before and after a specific substring in a line.

From the example below I want a way to find TEXT1 and TEXT2 programmatically. I assume I need to find the index of c:\ and then trim back till the first space is found. Then also find test\ and trim forward till the next space.

Note: There can be multiple instances of c:\test\test\ on the same line.

Other text for other things here blah blah TEXT1c:\test\test\ TEXT2 other garbage text here blah blah

I can trim on a found string X number of characters back and forth, but this is an unknown amount to the next space.

2

There are 2 answers

0
RetiredGeek On

This isn't elegant but it does work.

PS> $x = "This is junk Text1C:\Test\Test\ Text2 more junk"
#Find Text1 and add it's length to the answer
$y = $x.IndexOf("Text1") + 5
#Find Text2 and subtract $y go get length of substring
$Z = $x.IndexOf("Text2") - $y
#Extract your desired value and trim off trailing blanks
$ans = ($x.Substring($y,$z)).Trim()
$ans

Results:

C:\Test\Test\
0
LeeM On

If your random text includes non-alphanumeric or space characters, you may want to use .* (for any character) instead of each [\w\s]* sequence below.

$somepath = 'Other text for other things here blah blah TEXT1c:\test\test\ TEXT2 other garbage text here blah blah' -replace '^[\w\s]*TEXT1|\sTEXT2[\w\s]*$'
$somepath
C:\Test\Test\

That's assuming you want to get rid of the trailing space before TEXT2 as well. If not, just remove the \s immediately after the |. If there may or may not be a space in that position that you want to trim, the second part should be |\s?TEXT2[\w\s]*$