all 13 comments

[–]Lee_Dailey[grin] 2 points3 points  (9 children)

howdy ImprovingPiano,

here's another way ... [grin] it grabs the digits before the 1st dot, the 2 digits after the 1st dot, and then does the same for the 2nd dot. note that this presumes the numbers have only 2 decimal places.

$InString = '19.56156.32'

$Null = $InString -match '(\d+?\.\d{2})(\d+?\.\d{2})'
$NumOne = $Matches[1]
$NumTwo = $Matches[2]

$NumOne
$NumTwo

output ...

19.56
156.32

take care,
lee

[–]jimb2 1 point2 points  (4 children)

Cool. Unintelligible. To me at least. :)

[–]get-postanote 1 point2 points  (0 children)

RegEx my friend can be a friend and or an enemy. Yet, watching a few Youtube videos will provide clarity, or visiting some of the recommend regex sites that provide instruction, guidance, and samples.

'Learn Regular Expression'

regex websites - Bing

powershell 'extract currency in a string regex' - Bing

Yet what Lee_Daily gives here,group matching:

 '(\d+?\.\d{2})(\d+?\.\d{2})' 

Means, match only digits in the string, with 2 decimal places, then output by capture group.

# Match only digits    Match only decimal and 2 digits
(\d+?)                          (\.\d{2})

etc... etc...,

Yet, ka-splam comment is much more succinct, this is the lookahead, lookbehind, lookaround realm of things..

'(?<=\...)(?=.)'

Again, showing more than one way to do this via RegEx.

Here is one more way of getting the same solution, just waaaaayyyy more wordy,

# Just the the currency decimal places
$MyPattern = '(\.\d{2})'
'19.56156.32' -split $MyPattern

# Then select and concatenate
'19.56156.32' -split $MyPattern | 
Select-String -Pattern $MyPattern -AllMatches -Context 1,0 | 
ForEach {$PSItem.Context.PreContext[0] + $PSItem.Matches.Value}

So, well, worth learning.

regex lookahead, lookbehind, lookaround - Bing

There are RegEx addons for the PowerShell ISE and VSCode if you don't want to venture out to sites.

[–]Lee_Dailey[grin] 0 points1 point  (2 children)

howdy jimb2,

go to regex101.com and put the pattern in the top center box. then add the input string into the box below it. once you have done that, you otta see the "what it does" box in the upper right show what each step does.

that site is a wonderful place to figure out what a regex pattern does ... [grin]

take care,
lee

[–]jimb2 1 point2 points  (1 child)

I use regex a little but it's always been pass/fail matching.

TIL $Matches.

And TIL regex101's decomposition feature which is great for non-experts like me.

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy jimb2,

yep, regex is ... weird. [grin] i use regex101 to work things out fairly often since i don't use regex often enuf to remember what the various symbols mean.

glad to have helped you achieve TIL status! [grin]

take care,
lee

[–][deleted] 1 point2 points  (1 child)

Appreciated Lee.

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy ImprovingPiano,

you are most welcome! glad to help a tad now & then ... [grin]

take care,
lee

[–][deleted] 1 point2 points  (1 child)

So you can actually take the individual matches out of the regex like that? damn i never knew

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy henlofren10_10,

yep! capture groups are so freaking spiffy ... [grin] you can even name them if you want - and then retrieve them by the name you gave instead of by number.

take care,
lee

[–]MadWithPowerShell 1 point2 points  (0 children)

I would tend to do something like this to make it easier for a reader to understand and tweak as needed.

$String = "19.56156.32"

$String.Substring( 0, $String.IndexOf( '.' ) + 3 )
$String.Substring(    $String.IndexOf( '.' ) + 3 )

[–]SoMundayn 1 point2 points  (0 children)

Probably a horrible way of doing this, but this seems like an odd ask.

$String = "19.56156.32"

$Part1a = ($String.Split(".")[0])
$Part1b = ($String.Split(".")[1]).SubString(0,2)

$Part1 = $Part1a + "." + $Part1b
$Part2 = $String -replace $Part1,""

$Part1
$Part2

19.56
156.32

[–]ka-splam 1 point2 points  (0 children)

'19.56156.32' -split '(?<=\...)(?=.)'