mou
mou

Reputation: 73

java regular expression and replace all occurrences

I want to replace one string in a big string, but my regular expression is not proper I guess. So it's not working.

Main string is

Some sql part which is to be replaced

cond =  emp.EMAIL_ID = '[email protected]' 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);
        
        }

It's not working, not entering the loop at all. Any help is appreciated.

Upvotes: 2

Views: 229

Answers (2)

The fourth bird
The fourth bird

Reputation: 163342

Instead of the anchors ^ and $, you can use word boundaries \b to prevent a partial match.

If you want to match spaces on the same line, you can use \h to match horizontal whitespace char, as \s can also match a newline.

You can use replaceFirst on the string using $0 to get the full match, and an inline modifier (?i) for a case insensitive match.

Note that using [and|or] is a character class matching one of the listed chars and escape the dot to match it literally, or else . matches any char except a newline.

(?i)\bhemp\.EMPLOYEE_NAME\h*=\h*'\w+'\h+(?:and|or)\b

See a regex demo or a Java demo

For example

String regex = "\\bhemp\\.EMPLOYEE_NAME\\h*=\\h*'\\w+'\\h+(?:and|or)\\b";
String string = "cond =  emp.EMAIL_ID = '[email protected]' AND\n"
+ "emp.PERMANENT_ADDR LIKE('%98n%') \n"
+ "AND hemp.EMPLOYEE_NAME = 'xxx' and is_active='Y'";

System.out.println(string.replaceFirst(regex, "xx$0x"));

Output

cond =  emp.EMAIL_ID = '[email protected]' AND
emp.PERMANENT_ADDR LIKE('%98n%') 
AND xxhemp.EMPLOYEE_NAME = 'xxx' andx is_active='Y'

Upvotes: 0

rzwitserloot
rzwitserloot

Reputation: 102902

Obviously not - your regexp pattern doesn't make any sense.

  • The opening /: In some languages, regexps aren't strings and start with an opening slash. Java is not one of those languages, and it has nothing to do with regexps itself. So, this looks for a literal slash in that SQL, which isn't there, thus, failure.
  • ^ is regexpese for 'start of string'. Your string does not start with hemp.EMPLOYEE_NAME, so that also doesn't work. Get rid of both / and ^ here.
  • \\s is one whitespace character (there are many whitespace characters - this matches any one of them, exactly one though). Your string doesn't have any spaces. Your intent, surely, was \\s* which matches 0 to many of them, i.e.: \\s* is: "Whitespace is allowed here". \\s is: There must be exactly one whitespace character here. Make all the \\s in your regexp an \\s*.
  • \\w is exactly one 'word' character (which is more or less a letter or digit), you obviously wanted \\w*.
  • [and|or] this is regexpese for: "An a, or an n, or a d, or an o, or an r, or a pipe symbol". Clearly you were looking for (and|or) which is regexpese for: Either the sequence "and", or the sequence "or".
  • * - so you want 0 to many 'and' or 'or', which makes no sense.
  • closing slash: You don't want this.
  • closing $: You don't want this - it means 'end of string'. Your string didn't end here.

The code itself:

replaceFirst, itself, also does regexps. You don't want to double apply this stuff. That's not how you replace a found result.

This is what you wanted:

Matcher mat = pat1.matcher(cond);
mat.replaceFirst("replacement goes here");

where replacement can include references to groups in the match if you want to take parts of what you matched (i.e. don't use mat.group(), use those references).

More generally did you read any regexp tutorial, did any testing, or did any reading of the javadoc of Pattern and Matcher?

I've been developing for a few years. It's just personal experience, perhaps, but, reading is pretty fundamental.

Upvotes: 1

Related Questions