Search code examples
regexpcregrep

pcregrep: how to match only the first occurence?


I have a lot of files in the following format:

SIMPLE
{
    residualControl
    {
        p               1e-4;
        U               1e-4;
        "(k|omega|epsilon)" 1e-4;
    }
    nNonOrthogonalCorrectors 0;
    pRefCell        0;
    pRefValue       0;

}

potentialFlow
{
    nNonOrthogonalCorrectors 10;
}

relaxationFactors
{
    fields
    {
        p               0.3;
    }
    equations
    {
        U               0.7;
        "(k|omega|epsilon).*" 0.7;
    }
}

I want to match the following block of text in all files located in the folder $FOAM_TUTORIALS/incompressible:

    residualControl
    {
       // line 1
       // line 2
       // ...etc
    }

When I use pcregrep as follow:

    pcregrep --color -r -M "residualControl.*\n.*\{(.*\n)*" $FOAM_TUTORIALS/incompressible/

it matches the other lines too (see the comment below):

SIMPLE
{
    residualControl
    {
        p               1e-4;
        U               1e-4;
        "(k|omega|epsilon)" 1e-4;
    } // <<<<<<<<<<<<<<<<<<<<<<<<<<< I want it to stop match here. But it matches also the lines below
    nNonOrthogonalCorrectors 0;
    pRefCell        0;
    pRefValue       0;

}

potentialFlow
{
    nNonOrthogonalCorrectors 10;
}

relaxationFactors
{
    fields
    {
        p               0.3;
    }
    equations
    {
        U               0.7;
        "(k|omega|epsilon).*" 0.7;
    }
}

Could you please tell me how to modify the regex to match only the first block?


Solution

  • You may use this regex for search:

    pcregrep --color -r -M '^\h*residualControl\s*{[^}]*}' $FOAM_TUTORIALS/incompressible/
    

    residualControl
    {
        p               1e-4;
        U               1e-4;
        "(k|omega|epsilon)" 1e-4;
    }
    

    RegEx Details:

    • ^\h*: Match 0 or more horizontal whitespaces at a line start
    • residualControl\s*: Match string residualControl followed by 0 or more whitespaces (including newlines)
    • {: Match opening {
    • [^}]*: Match 0 or more characters (including newlines) that are not }
    • }: Match closing }