0

I'm new to Regex. I'm trying to scan few text file and get some field name based on Tag Field and Action. I'm using Powershell and regex = ?smi)\b(field|Action)[^)]+ to get the full Field and action String. Once i get the string now to get the value from field and action string i call (?<=;).* However it fails (wrong value) on line Field("Waiting Period (H)"; "Waiting Period (Hrs.)") { } . Please note the Value can be in Quote or no Quote however it will always end with this ) the problem is some field name can have e.g (Hrs.)")

{
    group("Default")
    {
       Field("Postal Code"; **Postal**)
        {
         ToolTip = 'Enter Postal Code ';
         trigger OnValidate()
         begin
          If ("Postal Code" <> xRec."Postal Code.") then
            function()
           END;
         }
        field ("Cels or Fahr"; **"Celsius or Fahrenheit"**) {
             ToolTip = 'Enter temperature ';
          }
        Field("Waiting Period (H)"; **"Waiting Period (Hrs.)"**) {  }
        Field ( Latitude; **Latitude**)  
         {
            ToolTip = 'Enter Latitude';
            trigger OnValidate()                   
          }
    }
}  
action(**"Restore to Default"**)
  {
  ToolTip = 'Enter Latitude';
}

I'm looking for output for tag "Field Or Action" to return these values below Postal "Celsius or Fahrenheit" "Waiting Period (Hrs.)" Latitude "Restore to Default"

So, If i use https://regex101.com/ on the same text (?<=;).* the text is "Waiting Period (Hrs.)") { } is wrong "Waiting Period (Hrs.)") { } . Any help or advice will be helpful.

1
  • Maybe regex (?smi)\b(field|Action)[^{]+ ( note [^{]+ instead of [^)]+). Commented May 19, 2020 at 13:02

1 Answer 1

0

First get the group

group("") {
  field(...) {}
}

by

$mainText -match '(?smi)\{(?<groupText>\s*?group\(".*"\)\s*?\{\s*?(?:\s*?Field\s*?\((?:\s*?"*?.*?(?:\(.*?\))*?.*?"*?)?;\s*?(?:\*\*"?.*?(?:\(.*?\))*?.*?"?\*\*)?\)\s*?\{.*?\}\s*?)*?\s*?\}\s*?)\}\s*?'

and get the groupText capture $groupText = $Matches["groupText"].

$fieldMatches = $groupText | select-string -Pattern '(?smi)(?:\s*?Field\s*?\((?<fieldName1>\s*?"*?.*?(?:\(.*?\))*?.*?"*?)?;\s*?(?<fieldName2>\*\*"?.*?(?:\(.*?\))*?.*?"?\*\*)?\)\s*?\{.*?\}\s*?)+?' -AllMatches

$fieldMatches.Matches | %{ $_.Groups["fieldName1"].Value + ' <===> '+ $_.Groups["fieldName2"].Value }

The point here is to use lazy matching with *?. And we use a separate match because when there is a repeated capturing groups in a pattern only the last capture matched.

You could similarly match the action group separately.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.