例如,将此视为string
a.prd_id IN
( SELECT prd_id FROM prd)
我需要在SELECT
ie之后prd_id
替换文本,并且不想更改后面的内容prd_id
"a.prd_id"
样本输出:
a.prd_id IN
( SELECT period FROM prd)
prd_id
在关键字之后的任何地方,'Select'
我都必须将其替换为'period'
你可以用积极的眼光来实现这一点。
(?i)(?<=SELECT\s)\s*prd_id\b
您将需要积极的回顾:
String s="a.prd_id IN\n" +
"( SELECT prd_id FROM prd)";
System.out.println("Replaced => " +
s.replaceAll("(?i)(?<=\\bSELECT)(\\s+)prd_id\\b", "$1period"));
输出:
Replaced => a.prd_id IN
( SELECT period FROM prd)
试试这个,
String inputString = "a.prd_id IN (SELECT prd_id FROM prd)";
String constantSelect = "SELECT";
String constantFrom = "FROM";
String tmpString = "", resultString = "";
int startIndex = inputString.indexOf(constantSelect);
int endIndex = inputString.indexOf(constantFrom);
tmpString = inputString.subString((startIndex + constantSelect.length), endIndex).trim;
resultString = inputString.replace(tmpString, "Your new string");
a.substring(a.indexOf("(")+1,a.indexOf(")")).replace("prd_id", "?") 其中a是你的字符串和'?' 将是您替换的字符串