0

I want regular expression to find & replace first and third rownum word in the below-given string with the word NONE.

String str = "select dummy,rownum,rowid as \"rownum\",rownum as order_number from dual";

I have tried below regular expression but it replacing the comma , before or after the word rownum

str = str.replaceAll("(?i)[^\"A-Z0-9]rownum[^$\"A-Z0-9]"," NONE ")

Actual Output : select dummy NONE rowid as "rownum" NONE as order_number from dual

Expected Output : select dummy,NONE, rowid as "rownum",NONE as order_number from dual

0

2 Answers 2

1

If you want to avoid replacing "rownum" because it is surrounded by " then you can use look-around mechanisms to forbid " before or after it like

String str = "select dummy,rownum,rowid as \"rownum\",rownum as order_number from dual";
str = str.replaceAll("(?i)(?!<\")\\brownum\\b(?!\")"," NONE ");
System.out.println(str);

Output: select dummy, NONE ,rowid as "rownum", NONE as order_number from dual

But that solution is valid only for this particular scenario. If you have other ones you should search for SQL parsers instead of RegEx.

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

Comments

0

You can try this, it's works

str = str.replace("rownum", "NONE");
str = str.replace("\"NONE\"", "\"rownum\"");

Comments

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.