Home > Software engineering >  Different results between regex101 and java code
Different results between regex101 and java code

Time:09-29

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

I have four matches.

But with my simple java code I have different output.

    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 ?

CodePudding user response:

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));
}
  • Related