第一次到csdn的java论坛里发帖,恳求高手帮忙解决一下。现在有这样的一个字符串: [code:and m.code in ('A1','A2','A3','A(T+5)','A9')]
替换成如下形式:and m.code in ('A1','A2','A3','A(T+5)','A9')
急求高手解决。。
谢谢先了
替换成如下形式:and m.code in ('A1','A2','A3','A(T+5)','A9')
急求高手解决。。
谢谢先了
楼主【lagon】截止到2008-07-08 09:19:26的历史汇总数据(不包括此帖):
发帖的总数量:1 发帖的总分数:60
结贴的总数量:1 结贴的总分数:60
无满意结贴数:0 无满意结贴分:0
未结的帖子数:0 未结的总分数:0
结贴的百分比:100.00% 结分的百分比:100.00%
无满意结贴率:0.00 % 无满意结分率:0.00 %
敬礼!
那substring就好了
String str="code:and m.code in ('A1','A2','A3','A(T+5)','A9')";
str=str.substring(5);
str.split(":");
str=str.replaceall("]"," ");
str=str.trim();
因为要替换的字符串是嵌在另外一个字符串里面的想用replaceall替换,但是('A1','A2','A3','A(T+5)','A9')括弧里面又有个括弧 始终是替换不掉
不知道怎么整!
不能用这种替换,因为要替换的字符串里面不止一个类似 [code:and m.code in ('A1','A2','A3','A(T+5)','A9')] 这种结构的,要通过判断一个一个替换掉的
和:
and m.code in ('A1','A2','A3','A(T+5)','A9')
括弧上有什么区别。
代码:
public class Test { public static void main(String[] args) {
String str = "[code:and m.code in ('A1','A2','A3','A(T+5)','A9')]";
System.out.println(str);
str= str.substring(6, str.length() - 1);
System.out.println(str);
}
}
结果:
[code:and m.code in ('A1','A2','A3','A(T+5)','A9')]
and m.code in ('A1','A2','A3','A(T+5)','A9')
不满足你的要求吗?
不能 用substring啊
因为要替换的字符串是嵌在另外一个字符串里面的 想用replaceall替换,但是('A1','A2','A3','A(T+5)','A9')括弧里面又有个括弧 始终是替换不掉
不知道怎么整!
各位达人有没有好的解决办法啊?
String a = “select m.* from m where 1=1 [code:and m.code in ('A1','A2','A3','A(T+5)','A9')] ”;现在想把a中的[code:and m.code in ('A1','A2','A3','A(T+5)','A9')] 替换成and m.code in ('A1','A2','A3','A(T+5)','A9')
如何替换?
现在就是通过这个参数b来决定如何替换的
1.如果b为空,a =" select m.* from m where 1=1";
2.如果b不为空,a= "select m.* from m where 1=1 and m.code in ('A1','A2','A3','A(T+5)','A9')
"就是这个样子的
str=str.replaceall("["," ");
str=str.replaceall("]"," ");
str=str.replaceall("code:"," "); //各种各样的类型 格式为 xxx: 可以使用switch 也可以使用全替换
str=str.replaceall("dode:"," ");
str=str.replaceall("ddde:"," ");
............
str=str.trim();
public class aa { /**
* @param args
*/
private String a = "[code:and m.code in ('A1','A2','A3','A(T+5)','A9')] ";
private String b = "('A1','A2','A3','A(T+5)','A9') ";
int temp = 0;
public String getString()
{
if(b != null || b.length() <1)
{
a = a.replace("[", "");
a = a.replace("]", "");
if(a.contains("code:"))
{
a = a.replace("code:", "");
}
}
return a.trim();
}
public static void main(String[] args) {
// TODO 自动生成方法存根
aa aa = new aa();
System.out.println(aa.getString());
}}
public class aa { /**
* @param args
*/
private String a = "[code:and m.code in ('A1','A2','A3','A(T+5)','A9')] ";
private String b = "('A1','A2','A3','A(T+5)','A9') ";
int temp = 0; public String getString()
{ if(b != null ¦ ¦ b.length() >1)
{
a = a.replace("[", "");
a = a.replace("]", ""); if(a.contains("code:"))
{
a = a.replace("code:", "");
} }
return a.trim();
}
public static void main(String[] args) {
// TODO 自动生成方法存根
aa aa = new aa();
System.out.println(aa.getString());
} }改下 是 >1 ~~
请问一下这个问题 应该怎么解决?
你能给数具体个例子吗?
比如我现在的这个问题,该怎么解决?
String a = "select * from m where 1=1 [code:and code in ('A1','A2','A(T+5)')]" +
"[id:and id in ('B1','B2','B(T+5)')]";//[...]可能有多个未知的条件
String b = "[code:and code in ('A1','A2','A(T+5)')]";//这只是个个例,
String c = "and code in ('A1','A2','A(T+5)')";
String d = "('A1','A2','A(T+5)";
String e = a.replaceAll(b,c);这种做法肯定是错的,请问你有什么好的办法可以达到我所表达出来的样子呢?
谁能给出个解决的具体例子吗?
比如现在的这个问题,该怎么解决?
String a = "select * from m where 1=1 [code:and code in ('A1','A2','A(T+5)')]" +
"[id:and id in ('B1','B2','B(T+5)')]";//[...]可能有多个未知的条件
String b = "[code:and code in ('A1','A2','A(T+5)')]";//这只是个个例,
String c = "and code in ('A1','A2','A(T+5)')";
String d = "('A1','A2','A(T+5)')";
String e = a.replaceAll(b,c); 这种做法肯定是错的,请问你有什么好的办法可以达到我所表达出来的样子呢?
最后得出来的结果应该是
"select * from m where 1=1 and code in ('A1','A2','A(T+5)')
and id in ('B1','B2','B(T+5)') and
String str = "[code:and code in ('A1','A2','A(T+5)')]";
Pattern pattern = Pattern.compile("\\[code:([^\\]]+)\\]",Pattern.CASE_INSENSITIVE|Pattern.DOTALL);
Matcher m = pattern.matcher(str);
while(m.find()){
System.out.println(m.group(1));
}
}
String str = "[code:and code in ('A1','A2','A(T+5)')]";
str=str.replaceAll("\\[code:", "").replaceAll("\\]", "");
System.out.println(str);
}
忘记说了一句,还有一种情况就是and code in ('A1','A2','A(T+5)'),括弧中的数据有可能为空
如果为空的话,就要把"[code:and code in ('A1','A2','A(T+5)')]"整个替换掉,而且code也不是个固定的字符串,可能为其他的字符如id,name
我是想"[code:and code in ('A1','A2','A(T+5)')]"整个替换掉
而你们只是想如何把[code:。]去掉唉,是我表达的不够清楚的原因?
String str = "[code:and code in ('A1','A2','A(T+5)')]";
Pattern pattern = Pattern.compile("\\[.*?(\\(.*\\))\\]",Pattern.CASE_INSENSITIVE|Pattern.DOTALL);
Matcher m = pattern.matcher(str);
while(m.find()){
if(m.group(1)!=null && m.group(1)!=""){
System.out.println("and m.code in "+m.group(1));//非空
}
else{
//为空处理
}
}
}
String str = "select m.* from m where 1=1 [code:and m.code in ('A1','A2','A3','A(T+5)','A9')]";
String regex = "\\[code:([^\\]]*)\\]";
str = str.replaceAll(regex, "$1");
System.out.println(str);
}
}
怎么到后面成了 [code:and code in ('A1','A2','A(T+5)')] 这样的?另外,[code: 与 and 后面的 code 有什么关系?
public class Test01 { public static void main(String[] args) {
String str1 = "select * from m where 1=1 [code:and code in ('A1','A2','A(T+5)')]" +
"[id:and id in ('B1','B2','B(T+5)')]";
String regex = "\\[(.*?):\\s*(and\\s+(?:[^\\] ]\\.)*\\1[^\\]]*)\\]";
str1 = str1.replaceAll(regex, " $2");
System.out.println(str1);
String str2 = "select * from m where 1=1 [code:and m.code in ('A1','A2','A(T+5)')]" +
"[id:and m.id in ('B1','B2','B(T+5)')]";
str2 = str2.replaceAll(regex, " $2");
System.out.println(str2);
}
}