Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex – capture group whish is optionally enclosed in sequence of characters

I have a file with lines I need to extract from the JSON-like syntax.
My regex works good in most cases. It extracts desired symbols into a second capture group. But I noticed sometimes my desired text is optionally can be enclosed by some tags which I want to ignore.

Sample file:

    {"title_available" "text1"}
    {"title_value" "<c(20a601)>text2"}
    {"tags"
        {"all" "text3"}
        {"ignore" "text4"}
        {"chargeFactor" "text5 %1%"}
        {"resourceSpeed" "%1% text6"}
    }
    {"rules" "bla-bla-bla\n\n \"BLA\" bla-bla-bla."}
            {"id1" "<c(c3baae)>text7</c>"}

My regex:
\s+{\"\S+\" \"(<c\(\S+\)>)?(.+)\"}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Desired output:

text1
text2
text3
text4
text5 %1%
%1% text6
bla-bla-bla\n\n \"BLA\" bla-bla-bla.
text7

Current output:

all ok except:
text7</c>

enter image description here

I guees I need to use a lookahead somehow with second capture group, but I didn’t find how. Also I’m not sure if I should use a capture group for skipping first optional <c…>. Can someone help with this pls?

P.S. speed or simplicity of the pattern doesn’t matter.

>Solution :

It seems like your regular expression is not excluding the closing tag </c> from the third capture group. To fix this, you can adjust your regex to exclude the closing tag if it’s present.

Like:

\s+{"\S+" "(?:<c\S+>)?(.+?)(?:<\/c>)?"}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading