3

I wonder if someone could shed some light on using a RegEx to extract some values.

I have a string that looks something like this:

$Aa37.33092,-2.9084$
  • The string will always start and end with a dollar-sign.
  • The two letters after the starting dollar-sign are irrelevant but will always be there.
  • Two numerical values follows, separated by a comma.
  • Both CAN be negative, but does not have to be.
  • Both CAN have up to three digits before the dot and up to five digits after the dot (the actual number of digits is unknown until the string arrives)

Each value should then go into a unique variable in PowerShell for further processing, but I think I can cope with the rest. I just need some hints or pointers on how to extract these values.

3
  • 3
    Have you tried simply splitting on commas? Commented Mar 8, 2013 at 10:18
  • 1
    ^\$(\w{2})(-?\d+\.\d+),(-?\d+\.\d+)\$$ Improved version of my previous pattern. Commented Mar 8, 2013 at 10:37
  • There's a double dollar at the end which I think shouldn't be there(?) Actually \w{2} is more die-hard correct (compared to Poorkenny's version) as I mentioned there will be exactly two letters. \D* is more forgiving. Thanks! Commented Mar 8, 2013 at 13:21

4 Answers 4

2

And yet another possibility:

$vals = '$Aa37.33092,-2.9084$' -split '[^--9]' -match '\S'
$val1,$val2 = $vals[0,1]
Sign up to request clarification or add additional context in comments.

1 Comment

Oh how simple, once you get it printed in front of you... I'll go for this one. Thanks!
2

Something like this:

$vals = [regex]::Matches('$Aa37.33092,-2.9084$','(-?\d*\.\d*)') | 
select -expa value 
$val1 = $vals[0]
$val2 = $vals[1]

using split method:

$vals =( ('$Aa37.33092,-2.9084$').Split(',') ) -replace '\$|[a-z]'
$val1 = $vals[0]
$val2 = $vals[1]

or using select-string

 '$Aa37.33092,-2.9084$' | select-string '(-?\d*\.\d*)' -AllMatches |
   Foreach {$_.matches} | select -expa value

1 Comment

Neat and tidy suggestions. Tested the select-string version with success. Thanks!
1

As I mention in my comment above, this regex seems to work (if anyone has a simpler solution, I'm interested)

"\$\D*(-?\d*[.]\d*),(-?\d*[.]\d*)\$"

So with a bit of PowerShell around it:

$sourceString = '$Aa37.33092,-2.9084$'
if($sourceString -match "\$\D*(-?\d*[.]\d*),(-?\d*[.]\d*)\$")
{
    $firstNumber = $matches[1]
    $secondNumber = $matches[2]
    Write-Host ("Here are the numbers: {0} - {1}" -f $firstNumber,$secondNumber)
}

1 Comment

Nice. And wrapped in perfectly understandable PS too! This works as requested. Thanks!
0

You are very close. Take a look at this page for debugging regular expressions and try out something simple like: \$\D*((-?\d*[.]\d*),)\$

2 Comments

I love how people who write regex always say "Look, it's simple: <insert random character sequence here>" :D (Don't mistake me, I do write regex too, and I also do that...) And your regex doesn't seem to work (only attempts to match a single number). "\$\D*(-?\d*[.]\d*),(-?\d*[.]\d*)\$" is more complicated but works, don't know if there's a simpler way to write it though...
I should have mentioned that I am a complete noob at regex... I liked this one, because it's complex enough to impress anyone who will read the code.. :) But, as mentioned. It does not work as-is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.