最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Powershell -match returning System.Collections.Hashtable[1] in $Matches - Stack Overflow

programmeradmin3浏览0评论

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
  • You're looking to get <my company name> in the capturing group 1 or what was the expectation here? – Santiago Squarzon Commented Mar 5 at 19:28
  • It's unclear if your question is why -match automatically populates the $Matches automatic variable or why your pattern isn't capturing <my company name> in the capturing group 1. Can you please clarify? – Santiago Squarzon Commented Mar 5 at 19:44
  • @SantiagoSquarzon No problem clarifying. You guessed right with your first comment. – user66001 Commented Mar 5 at 19:49
  • 1 Are you inspecting the value of $Matches[1] with Write-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
  • Am aware of the need for $() 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
 |  Show 2 more comments

1 Answer 1

Reset to default 2

Assuming 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>
}
发布评论

评论列表(0)

  1. 暂无评论