Link to home
Start Free TrialLog in
Avatar of c7c4c7
c7c4c7Flag for United States of America

asked on

Need help using powershell to search a string

I am using powershell to search a string looking for a particular value, the string is part of a URL
Lets say the string consists of characters like this

"GFiYXNlaWQ9NTAwJmFtcDtzZXJpYWw9MTY4NTEwODQmYW1wO2Vt      Experts-Exchange               14.92"

I'm using Indexof to find Experts-Exchange, then based off of that location I want to find the start of the decimal number after the white spaces.  I cannot figure out how to do that, any suggestions?
Avatar of oBdA
oBdA

You can extract that value with a Regular Expression.
This will match "Experts-Exchange", followed by any number of spaces, and then matches any amount of digits or dots.
The match will be in a named group "Value"
If you can give us more information about the exact expected/allowed format of the value you want, the RegEx could be more specific than just "any amount of digits or dots".
Experts-Exchange\s*(?<Value>[0-9.]+)

Open in new window

"GFiYXNlaWQ9NTAwJmFtcDtzZXJpYWw9MTY4NTEwODQmYW1wO2Vt      Experts-Exchange               14.92" -match 'Experts-Exchange\s+(?<Value>[0-9.]+)'
$value = $Matches['Value']
$value

Open in new window

Avatar of c7c4c7

ASKER

It will be one of the following
x.xx
xx.xx
xxx.xx
Then the RegEx would be
Experts-Exchange\s+(?<Value>\d{1,3}\.\d{2})(\D|$)

Open in new window

1-3 digits, followed by a dot, followed by exactly two digits, followed by either a non-digit or the end of the string.
If the match evaluates to $false, the pattern couldn't be found.
"GFiYXNlaWQ9NTAwJmFtcDtzZXJpYWw9MTY4NTEwODQmYW1wO2Vt      Experts-Exchange               14.92" -match 'Experts-Exchange\s+(?<Value>\d{1,3}\.\d{2})(\D|$)'
$value = $Matches['Value']
$value

Open in new window

What would be the full URL, not just the string?
Avatar of c7c4c7

ASKER

The URL changes, so you cannot search based off the full content of the URL.  I'm looking for specific characters in the URL

oBDA
That worked great, the only question I have is why when I put the search string into a variable, why it doe not find it

So If I change your code to

$search = "Experts-Exchange"
"GFiYXNlaWQ9NTAwJmFtcDtzZXJpYWw9MTY4NTEwODQmYW1wO2Vt      Experts-Exchange               14.92" -match '$search\s+(?<Value>\d{1,3}\.\d{2})(\D|$)'

It does not find it
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'm not trying to base the search off the full content.  I want to know the context of the string you are showing us versus the entire (representative sample) URL.
Avatar of c7c4c7

ASKER

Thanks for the help that worked perfectly