lundi 3 mai 2021

java regular expression and replace all occurances

I want to replace one string in a big string. But reg is not proper I guess. So its not working.

Main string is

Some sql part which is to be replaced

cond =  emp.EMAIL_ID = 'xx@xx.com' AND
emp.PERMANENT_ADDR LIKE('%98n%') 
AND hemp.EMPLOYEE_NAME = 'xxx' and is_active='Y'

String to find and replace is

Based on some condition sql part to be replaced

hemp.EMPLOYEE_NAME = 'xxx'

I have tried this with Pattern and Matcher class is used and

Pattern pat1 = Pattern.compile("/^hemp.EMPLOYEE_NAME\\s=\\s\'\\w\'\\s[and|or]*/$", Pattern.CASE_INSENSITIVE);
        
        Matcher mat = pat1.matcher(cond);
         while (mat.find()) {
                     System.out.println("Match: " + mat.group());
                    cond = mat.replaceFirst("xx "+mat.group()+"x");
                    mat = pat1.matcher(cond);
        
        }

Its not working, not entering the loop at all. Any help is appreciated.

Aucun commentaire:

Enregistrer un commentaire