2

I'm trying to parse thousands of Oracle SQL script WHERE clauses.

I want to replace the keyword user (as in the person currently logged in) with something else. I can't just do a replace all as user may be used as part of another word in the where clause like a column name, for example, payuser. And I can't simply check is there whitespace on either side of it as it could be inside a bracket or have an equals sign next to it or some other mathematical character.

where (username = user or admin = user) and loggedIn=user order by pay_group_code;

public class TestClass{

     public static void main(String []args){

        String where = "where (username = user or admin = user) and loggedIn=user  order by pay_group_code;";

        where = where.replaceAll("\buser\b", "test");

        System.out.println(where);
     }
}
1
  • can you provide a sample input and desired output? Commented Jun 5, 2015 at 13:45

1 Answer 1

3

Inside a replaceAll, you need to use double escape symbols:

where = where.replaceAll("(?i)\\buser\\b", "test");

Without the double escapes, you match a backspace BS symbol.

See demo

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I'm also now using (?i) to ignore case so that the regex looks like "(?i)\\buser\\b".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.