I haven't the faintest what is going on here, so I am turning it over to the cloud mind to figure out, sorry.
I have a string
(confirmed with .gettype()
), an excerpt of which is below (\r\n at the end of each line):
Request Inputs
Inputs With Value
Displaying 17 out of 19 inputs
Company Division
<my company name>
However when run against the below pattern, cannot figure out why it produces System.Collections.Hastable[1]
in $Matches[1]
. And the main thing, it is empty. Why?
$TextBoxText -match '(?s).*(?:Company Division[\r\n])([^\r\n]*)'
I haven't the faintest what is going on here, so I am turning it over to the cloud mind to figure out, sorry.
I have a string
(confirmed with .gettype()
), an excerpt of which is below (\r\n at the end of each line):
Request Inputs
Inputs With Value
Displaying 17 out of 19 inputs
Company Division
<my company name>
However when run against the below pattern, cannot figure out why it produces System.Collections.Hastable[1]
in $Matches[1]
. And the main thing, it is empty. Why?
$TextBoxText -match '(?s).*(?:Company Division[\r\n])([^\r\n]*)'
Share
Improve this question
asked Mar 5 at 19:22
user66001user66001
9151 gold badge15 silver badges37 bronze badges
7
|
Show 2 more comments
1 Answer
Reset to default 2Assuming you're wondering why your current pattern didn't capture <my company name>
in the capturing group 1
, the reason is because in Windows, the strings new lines will usually be CRLF and with your current character class [\r\n]
you're allowing both, CR and LF, but only a single match of any of them. So you could do:
if ($TextBoxText -match '(?s).*Company Division[\r\n]{1,2}([^\r\n]*)') {
$Matches[1] # <my company name>
}
Then the pattern works fine, but even better would be to use \r?\n
which would also work well in any OS:
if ($TextBoxText -match '(?s).*Company Division\r?\n([^\r\n]*)') {
$Matches[1] # <my company name>
}
In both cases the non-capturing group can be removed, there is no reason to have it there. You could also remove (?s).*
from the pattern, leaving it for now as it's unclear what was the intent. If you're only interested in capturing what comes after Company Division
, then the pattern could just be:
if ($TextBoxText -match '(?<=Company Division\r?\n)[^\r\n]*') {
$Matches[0] # <my company name>
}
<my company name>
in the capturing group1
or what was the expectation here? – Santiago Squarzon Commented Mar 5 at 19:28-match
automatically populates the$Matches
automatic variable or why your pattern isn't capturing<my company name>
in the capturing group1
. Can you please clarify? – Santiago Squarzon Commented Mar 5 at 19:44$Matches[1]
withWrite-Host "$Matches[1]"
or similar? It's a string interpolation issue,$Matches
is evaluated separately and[1]
interpreted as text :) – Mathias R. Jessen Commented Mar 5 at 19:50$()
in the situation you mention. This is what I get (Sorry, can't use the likes of Imgur at work) when issuing $Matches at the command line. Name Value ---- ----- 1 0 Request Inputs... – user66001 Commented Mar 5 at 19:54