我想替换不在引号之间的单词。
应该如何工作:
“英雄”-not change
“你是超级英雄”--不是改变
我是英雄-改变
你是超级英雄-改变
我试过了
word = word.replaceAll("(?!\")(.*)hero(.*)(?!\")","$1 zero $2") -但它不能工作
发布于 2013-06-28 05:44:29
这个正则表达式应该适用于您:
hero(?=(?:(?:[^\"]*\"){2})*[^\"]*$)解释:,它基本上意味着匹配文字文本(hero),如果它后面跟着偶数个双引号,换句话说,如果它没有双引号,则匹配所需的文本。
测试:
String str = "hero \"dont-hero\"";
String repl = str.replaceAll("hero(?=(?:(?:[^\"]*\"){2})*[^\"]*$)", "FOO");
//repl = FOO "dont-hero"现场演示:http://ideone.com/BXJxpw
发布于 2013-06-28 05:51:16
你的问题不清楚。你想处理任意数量的嵌套引号吗?如果是这样,又是如何做到的呢?例如,对于以下输入,您希望发生什么情况?
I hate it when people say "you are such a "hero"!"“英雄”这个词在这里有引号吗?或者是用引号引起来的短语“你就是这样的人”,后面是不带引号的单词“英雄”,然后是"!“在引号中?
但即使是一个更简单的例子
"hello" said the boring old "cat", are you really a "hero"?使用正则表达式可能无法实现,至少不是以一种合理的方式来实现,这将使它变得值得。
被接受的答案以一种完全不直观的方式在
I will be your "hero" baby! O"RLY?为什么不直接用一些代码来解析这个字符串呢?
我觉得下面是比使用正则表达式更好的选择:
class Main {
public static String replace(String str, String origstr, String newstr) {
StringBuilder result = new StringBuilder();
int lastIdx = 0;
boolean inquotes = false;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == '"') {
if (inquotes) {
inquotes = false;
result.append(str.substring(lastIdx, i+1));
} else {
result.append(str.substring(lastIdx, i+1).replace(origstr, newstr));
inquotes = true;
}
lastIdx = i+1;
}
}
result.append(str.substring(lastIdx, str.length()).replace(origstr, newstr));
return result.toString();
}
public static void main (String[] args) throws java.lang.Exception {
System.out.println(replace("", "change", "___"));
System.out.println(replace("\"dont-change\"", "change", "___"));
System.out.println(replace("\"change", "change", "___"));
System.out.println(replace("simple: change", "change", "___"));
System.out.println(replace("simple2: \"dont-change\"", "change", "___"));
System.out.println(replace("change \"dont-change\"\"", "change", "___"));
System.out.println(replace("change \"dont-change\"", "change", "___"));
System.out.println(replace("\"dont-change\" change", "change", "___"));
}
}发布于 2013-06-28 06:23:35
一种使用正则表达式的方法。
这个想法是在我放在捕获组中的目标单词之前匹配引号之间的所有子字符串。然后用捕获组偏移量替换包含目标单词的子串。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class quotyquot {
public static void main(String[] args) {
String s = "I am super hero here and now\n"
+"Superman is an incredible hero\n"
+"I am super \"hero\" here and now\n"
+"\"I am super hero here and now\"";
String t = "hero"; // target
String r = "zero"; // replacement
Integer d = r.length() - t.length();
Integer o = 0; // offset
Pattern p = Pattern.compile("\"[^\"]*\"|(" + t + ")");
Matcher m = p.matcher(s);
while (m.find()) {
if (m.group(1)!=null) {
s= s.substring(0, m.start() - o) + r + s.substring(m.end() - o);
o -= d;
}
}
System.out.println(s);
}
}使用split的另一种方式:
String t = "hero"; // target
String r = "zero"; // replacement
int c=0; // switch
String[] pi = s.split("(?=hero|\")|(?<=hero|\")");
String result = "";
for (int i=0; i<pi.length;i++) {
if (c==0 && pi[i].equals(t))
pi[i]=r;
else if (pi[i].equals("\""))
c = 1 - c;
result += pi[i];
}
System.out.println(result);https://stackoverflow.com/questions/17353269
复制相似问题