Play with Regex in Powershell

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

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

    Hi all,

    I am new to Regex trying to learn about it. I have the below data.

    $Data = " 1.4G /ofs01/prod/FileStore_auto_1"

    The value in 1.4G which Should never exceed more than 2.5G while the rest data will be same. Please help me to write a regex for the same.

  • #57418
    Profile photo of Amar Helloween
    Amar Helloween
    Participant

    Executed this, it gave me 1.4 hope i m doing it in right way.

    [Regex]::Match($Command,"\d\.\d").Value

    • #57421
      Profile photo of Olaf Soyk
      Olaf Soyk
      Participant

      If it does the job and that is what you expected ... thumbs up 😉

  • #57425
    Profile photo of Ron
    Ron
    Participant

    I have found this page quite valuable for developing and testing regex expressions.

    https://regex101.com/

    I'm sure there are others out there, but this one works for me. It allows you to test your expression with variations on your data to make sure you getting exactly the results you want, and displays diagnostic information on how it got those results.

    You didn't give a lot of information on how your data can vary, but my first instinct would be to make it as fuzzy as possible without failing. Your regex works as long as there is always one single digit, a period, and one single digit. I assume this is a storage size or something similar. You mentioned a max size, but can it be less? Will it always display a single decimal place? If its less than 1.0 will it display the leading zero? Is it always provided as "G" (gigabytes?)? And so on.

    Edit: Fixed link

  • #57428
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    Ron, missing your "this page" link

    • #57430
      Profile photo of Ron
      Ron
      Participant

      Looks like the link prompter doesn't automatically provide the url as the text if you leave the text blank. I updated it. Thanks

You must be logged in to reply to this topic.