c7c4c7
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
"GFiYXNlaWQ9NTAwJmFtcDtzZX JpYWw9MTY4 NTEwODQmYW 1wO2Vt 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?
Lets say the string consists of characters like this
"GFiYXNlaWQ9NTAwJmFtcDtzZX
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?
ASKER
It will be one of the following
x.xx
xx.xx
xxx.xx
x.xx
xx.xx
xxx.xx
Then the RegEx would be
If the match evaluates to $false, the pattern couldn't be found.
Experts-Exchange\s+(?<Value>\d{1,3}\.\d{2})(\D|$)
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
What would be the full URL, not just the string?
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"
"GFiYXNlaWQ9NTAwJmFtcDtzZX JpYWw9MTY4 NTEwODQmYW 1wO2Vt Experts-Exchange 14.92" -match '$search\s+(?<Value>\d{1,3 }\.\d{2})( \D|$)'
It does not find it
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"
"GFiYXNlaWQ9NTAwJmFtcDtzZX
It does not find it
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
ASKER
Thanks for the help that worked perfectly
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".
Open in new window
Open in new window