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

Different results between regex101 and java code

I have this regex: https://regex101.com/r/vxHtzh/1

I have four matches.

But with my simple java code I have different output.

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

    Pattern pattern = Pattern.compile("'([0-9a-zA-Z-[^\\x00-\\x7F]+ ]+(',')*)'",Pattern.MULTILINE);

    String line =  "( $th$.t == 'Vision'e' )  ||  ( $g$.rfd == 'servizio visione ù è al estero' )  ||  ( $b$.fg == 'città diversa nazionalita'' )  ||  ( $mh$.fgh != 'l'installazione di servizi e un po' )";

    Matcher m = pattern.matcher(line);

    while (m.find()) {
        System.out.println(m.group(0));
    }

Output:

'Vision'
'servizio visione ù è al estero'
'città diversa nazionalita'
'l'

Where am I doing wrong ?

>Solution :

To escape '\' in regex you added one more '\'. But in the java string, you need to add '\' 4 times to match the actual regex.

Pattern pattern = Pattern.compile("'([0-9a-zA-Z-[^\\\\x00-\\\\x7F]+ ]+(',')*)'",Pattern.MULTILINE);

String line =  "( $th$.t == 'Vision'e' )  ||  ( $g$.rfd == 'servizio visione ù è al estero' )  ||  ( $b$.fg == 'città diversa nazionalita'' )  ||  ( $mh$.fgh != 'l'installazione di servizi e un po' )";

Matcher m = pattern.matcher(line);

while (m.find()) {
    System.out.println(m.group(0));
}
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