'Powershell Regex to find number pattern

Heyho,

I'm pretty new to using regex, and I can't seem to find the right regex to match a pattern like this:

1000.00

2093.09

####.##

the numbers could be anything but the digits are always 4 before the dot and 2 after. Thanks for the help!



Solution 1:[1]

Use [0-9] to describe a single digit 0 through 9, then a bounded quantifier {N} to specify how many:

[0-9]{4}\.[0-9]{2}

PowerShell's regex operators (-match, -notmatch, -replace, -split) apply partial matching by default, so make sure you anchor your pattern with \b (word boundary) or ^/$ (start of string, end of string):

'2093.09' -match '^[0-9]{4}\.[0-9]{2}$'
# or
'The number is 1234.56 or thereabout' -match '\b[0-9]{4}\.[0-9]{2}\b'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mklement0