kata
kata

Reputation: 53

PowerShell - Problems at using match if the text I need is before the keyword

My problem is that if I write $var -match "(id="".*?$args)", I get everything from the first id of the text to $args, but I only need text starting from the id which is closest to $args.

Any help would be much appreciated.

Upvotes: 1

Views: 81

Answers (1)

nkasco
nkasco

Reputation: 336

Assuming $var has no properties we can grab, if so just use select/where, otherwise what you can try is finding the start index of $args, then if the IDs are the same length each time you could grab X number of characters before $args.

#So it would look something like: ID=xxxxxx$args
$argsIndex = $var.IndexOf($args)

#Lets say the ID is always 6 chars long, grab 6 characters prior using the substring method:    
$var.substring($argsIndex-6,6)

Upvotes: 1

Related Questions