Regex in Powershell

This topic contains 7 replies, has 4 voices, and was last updated by Profile photo of Ron Ron 3 weeks ago.

  • Author
    Posts
  • #57556
    Profile photo of Amar Helloween
    Amar Helloween
    Participant

    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 🙂

  • #57562
    Profile photo of Olaf Soyk
    Olaf Soyk
    Participant

    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

    • #57568
      Profile photo of Amar Helloween
      Amar Helloween
      Participant

      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.

    • #57569
      Profile photo of Peter Jurgens
      Peter Jurgens
      Participant

      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...

    • #57571
      Profile photo of Olaf Soyk
      Olaf Soyk
      Participant

      OK OK ... so this should work then

      $Output -match "(\d+\.*\d*)(M|G)"
    • #57572
      Profile photo of Amar Helloween
      Amar Helloween
      Participant

      Thanks Olaf, That worked fine 🙂

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

    • #57574
      Profile photo of Olaf Soyk
      Olaf Soyk
      Participant

      1. You're wlcome. 😉
      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 😉 ) 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

  • #57623
    Profile photo of Ron
    Ron
    Participant

    Take a look here.

You must be logged in to reply to this topic.