gooly
gooly

Reputation: 1341

Program-Name Detection

this is how the lines look like:

  //|                                                        Vegas.c |

and I would like to get the name, here Vegas.c
This works in PS' regex:

  $found = $body -match '.+?\s+(\w.+?\.c[\+]+)[\s\|]+'

But what if the name does not start with a-zA-Z0-9 (=\w) but e.g. ~ or other none-word-chars?
The first char of the name must be different from a blank so I tried:

  $found = $body -match '.+?\s+(\S+.+?\.c[\+]+)[\s\|]+'
  $found = $body -match '.+?\s+([^\ ]+.+?\.c[\+]+)[\s\|]+'
  $found = $body -match '.+?\s+([^\s]+.+?\.c[\+]+)[\s\|]+'

None of them work even some more work. In most of the cases this detects only the whole line!

Any ideas?

Upvotes: 0

Views: 83

Answers (2)

Matt
Matt

Reputation: 46710

I think you made your question more basic then you needed from what I see in your comments but I have this which worked with your test string.

$string = @"
      //|                                                        Vegas.c |
"@

Just look for data inbetween the pipes and whitespace the pipes border. Not sure how it will perform with you real data but should work if spaces are in the program names.

[void]($string -match "\|\s+(.+)\s+\|")
$Matches[1]
Vegas.c

You could also used named matches in PowerShell

[void]($string -match "\|\s+(?<Program>.+)\s+\|")
$Matches.Program
Vegas.c

Upvotes: 0

Stephen
Stephen

Reputation: 4249

How about this?

 \/\/\| *([^ ]*)

\/ matches the character /
\/ matches the character /
\| matches the character |
 * matches 0 to many of the character
round brackets ( ) are the first capture group
[^ ] captures all the characters that are ^(not) a space (so long as all your file names do not contain spaces this should work)

Upvotes: 1

Related Questions