regex little frustation

Welcome Forums General PowerShell Q&A regex little frustation

Viewing 6 reply threads
  • Author
    Posts
    • #256952
      Participant
      Topics: 5
      Replies: 4
      Points: 32
      Rank: Member

      Hello,

      I was trying to get this right but I am not able to get the output in the way i want it.

      I want to extract all the parameters which are parsed to this command as a regex

      “C:\Program Files (x86)\AVEVA\Everything3D2.10\mon.exe” PROD E3D INIT T:\Scripts\Design\launch.init TTY MOA HZIENG/ENG /EXPORT $M/\\sevrer.blablabla\dfs\EXPORT.mac

      If i do something like this  -match ‘(PROD.*$)’ then it works.

      But what i was hoping was to match from the end of the string till the first quote  ‘ ” ‘ and this is where i didnt get my way around. I would appreciate your help.

      Thanks a lot.
      Cheers

    • #256997
      Participant
      Topics: 5
      Replies: 341
      Points: 585
      Helping Hand
      Rank: Major Contributor

      What output are you looking for? This should give you an array of strings containing each parameter.

    • #257006
      Participant
      Topics: 1
      Replies: 85
      Points: 387
      Helping Hand
      Rank: Contributor

      You can use a positive lookbehind (?<=) to validate a starting double quote ( ^)  followed by non-double quotes ( [^]+) and a closing double quote. \s+ will match any spaces after the closing double quote.

    • #257096
      Participant
      Topics: 5
      Replies: 4
      Points: 32
      Rank: Member

      Hello,

      Thanks for the feedback.

      “random commandline” yes,  this works. but actually my intention is not to match against the word “PROD” but to the ‘ ” ‘

      @AdminOfThings45. Thanks for your example. It works great.
      But your logic is harder to understand for me.
      My logic was starting from the end until it matches the first quote.
      So using the .*$ and to stop once when it finds the first ‘”‘ and this what I didnt manage to get it working in regex.
      Do you know how this could be done?

      Anyway, thanks a lot for your time and to get me on the right path.

      Regards,
      Jacobo.

       

       

       

    • #257105
      Participant
      Topics: 9
      Replies: 674
      Points: 2,664
      Helping Hand
      Rank: Community Hero

      Would it not be an option to just do this?

    • #257201
      Participant
      Topics: 1
      Replies: 85
      Points: 387
      Helping Hand
      Rank: Contributor

      The problem with pattern matching is that you need to understand your data. Doug’s examples work when there’s only one pair of double quotes. Since PowerShell and .NET don’t support match resets in regular expressions, it makes the regex string more verbose.

      The reason

      won’t work is because the first double quote is matched. Then .*$ matches everything else until the end of the string. You will need to add something like a positive lookbehind to verify that only the second double quote matches. If there’s only the one pair of double quotes, a close alternative is

      .

      Using -replace will be less cumbersome than -match if you are just trying to reduce the command string.

    • #257231
      Participant
      Topics: 8
      Replies: 168
      Points: 817
      Helping Hand
      Rank: Major Contributor

      And yet another way …

      $command.SubString($command.LastIndexOf('"') + 1)

Viewing 6 reply threads
  • You must be logged in to reply to this topic.