Regex in Powershell

Hi,

I am new to Regex world, below is my requirement.

$Output = " 2.1G /ofs01/prod/OFSFileStore_auto_1"

I need to extract the 2.1G from the data, so I did the below thing.

[Regex]::Match($Output,"\d+.\d+[M|G]).Value

It gave me the output 2.1G which is perfect, but now the Condition is :

  1. 2.1G Value should not be greater than 2.5G
  2. It is not necessary that it will be in Gb only, it can be 385M or 385.27M like this
  3. So first thing is I need to extract the (value+GB or MB). If its in GB then will start comparing else i will need to convert it to GB and then compare with 2.5 GB

So kindly help me to provide any regex or solution as per the points mentioned above.

Thanks :slight_smile:

If you do it like this:

$Output -match “(\d+.*\d+)([M|G])”

you will have the number in $Matches[1] and the G or M in $Matches[2] so you can check and proceed accordingly

Olaf Thanks for the reply. but in case the Output doesn’t have a decimal value. it will return False.

Example :

$Output = " 2G /ofs01/prod/OFSFileStore_auto_1"

return false

Can u guide further on this.

Hi Olaf,

While your solution should work for the OP I wanted to point out the possible misunderstanding of the characters in yours and the OP’s regex pattern. The square brackets are used to identify a range of characters or multiple characters to match. Therefore with the way both of you have written [M|G] the pipe character does not mean “or”, rather it is a character that can be matched in the string. Instead of the square brackets simply use regular brackets/parenthesis to group characters in an “or” statement separated by the pipe character. To use your example I’d simply remove just the square brackets from your pattern to get the desired result.

I think I’m this specific case it may not be a problem, however you may get undesired results say if for example the is no M or G in the place you expect, and there is a pipe character further along in the string, with all digits before it. That may come up as a match with your regex pattern.

Otherwise I like your use of capture groups, and my only other comment would be to follow the capture group with a unique character that is known to immediately follow the desired characters to ensure you don’t capture more than you expect. + and * can be “greedy” and match characters past where you intended. Hence there is also the ? Qualifier for + and * which tell them not to be “greedy” and stop at the first instance of the next character in your pattern.

I hope I’ve explained my point well enough as after reading it again it seems like gibberish but this happens a lot with regex…

OK OK … so this should work then

$Output -match “(\d+.\d)(M|G)”

Thanks Olaf, That worked fine :slight_smile:

Can you suggest on this : https://powershell.org/forums/topic/extacting-capacity-info-using-powershell-regex/

  1. You’re wlcome. :wink:
  2. Well I could … but first - I don’t understand your question completely (englich is not my native language as well) and second - I don’t like to write scripts on request. ( Usually I get paid for that :wink: ) If you have some code to show where you have problems or errors you could show it here and we will try to help you. For prewritten scripts you can search here: Microsoft Technet Script Gallery

Take a look here.