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

How parse key-value with regex

i use Kotlin \ Java for parse some string.

My regex:
\[\'(.*?)[\]]=\'(.*?)(?!\,)[\']

text for parse:

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

someArray1['key1'] = 'value1', someArray2['key2'] = 'value2', ignoreText=ignore, some['key3'] = 'value3', ignoreMe['ignore']=ignore, some['key4'] = 'value4'..

i need result:

key1=value1
key2=value2
key3=value3
key4=value4

Thanks for help

>Solution :

Another regex for you

\['(\w+)'\]\s+(=)\s+'(\w+)'

Regex101 Demo Fiddle

Java test code

    String str = "someArray1['key1'] = 'value1', someArray2['key2'] = 'value2', ignoreText=ignore, some['key3'] = 'value3', ignoreMe['ignore']=ignore, some['key4'] = 'value4'..";
    String regex = "\\['(\\w+)'\\]\\s+(=)\\s+'(\\w+)'";
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(str);
    while (matcher.find()) {
        System.out.println(matcher.group(1) + matcher.group(2) + matcher.group(3));
    }

Test result:

key1=value1
key2=value2
key3=value3
key4=value4
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