2

例如,将此视为string

a.prd_id IN     
(  SELECT prd_id FROM  prd)

我需要在SELECTie之后prd_id替换文本,并且不想更改后面的内容prd_id"a.prd_id"

样本输出:

a.prd_id IN     
(  SELECT period FROM  prd)

prd_id在关键字之后的任何地方,'Select'我都必须将其替换为'period'

4

4 回答 4

4

你可以用积极的眼光来实现这一点。

(?i)(?<=SELECT\s)\s*prd_id\b
于 2013-08-14T11:37:47.877 回答
1

您将需要积极的回顾:

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)
于 2013-08-14T11:43:05.723 回答
0

试试这个,

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");
于 2013-08-14T11:34:16.420 回答
0

a.substring(a.indexOf("(")+1,a.indexOf(")")).replace("prd_id", "?") 其中a是你的字符串和'?' 将是您替换的字符串

于 2013-08-14T11:34:51.290 回答