如何替换行情之外的单词

时间:2022-12-27 19:21:27

I would like to replace strings outside of quotes using str.replaceAll in Java but to leave words inside quotes untouched

我想在Java中使用str.replaceAll替换引号之外的字符串,但是不要触及引号内的单词

If I replaced Apple with Pie:

如果我用Pie代替Apple:

Input: Apple "Apple Apple Apple"
Desired Output: Pie "Apple Apple Apple"

输入:Apple“Apple Apple Apple”所需产量:Pie“Apple Apple Apple”

Note the words inside quotes were untouched

请注意引号内的单词不受影响

How would this be done? All help Appreciated!

这怎么办?所有帮助赞赏!

5 个解决方案

#1


6  

Search for Apple using lookaheads to make sure it is not surrounded by quotes:

使用前瞻搜索Apple以确保它不被引号括起:

(?=(([^"]*"){2})*[^"]*$)Apple

And replace by:

并替换为:

Pie

RegEx Demo

RegEx演示


Update:

更新:

Based on comments below you can use:

根据以下评论,您可以使用:

Code:

码:

String str = "Apple \"Apple\"";
String repl = str.replaceAll("(?=(([^\"]*\"){2})*[^\"]*$)Apple", "Pie");
//=> Pie "Apple" "Another Apple Apple Apple" Pie

#2


0  

I assume this is what you want:

我认为这是你想要的:

String str = "Apple \"Apple\"";
String replace = str.replaceAll("(?<!\")Apple(?!\")", "Pie");

Here is the working:https://regex101.com/r/kP0oV1/2

这是工作:https://regex101.com/r/kP0oV1/2

#3


0  

This works for your test:

这适用于您的测试:

package mavensandbox;


import static junit.framework.TestCase.assertEquals;

public class Test {

    @org.junit.Test
    public void testName() throws Exception {
        String input = "Apple(\"Apple\")";
        String output = replaceThoseWithoutQuotes("Apple", "Pie", input);
        assertEquals("Pie(\"Apple\")", output);
    }

    private String replaceThoseWithoutQuotes(String replace, String with, String input) {
        return input.replaceAll("(?<!\")" + replace + "(?!\")", with);
    }
}

I'm using what's called a negative lookahead and a negative lookbehind. It finds matches that don't have a " in front or behind of it. Does that work for you?

我正在使用所谓的负向前瞻和负面的后视。它找到没有“在它前面或后面的匹配。这对你有用吗?

#4


0  

Try matching the word with whitespace after it.

尝试将单词与后面的空格匹配。

/Apple\s/

/苹果/

Then replace with Pie with the same whitespace after it.

然后用Pie替换相同的空格。

#5


0  

There's also an option to do this without a more complicated regex if you're looking for a more iterative solution, if you're so inclined. You can split on " and replace even-numbered indexes and then rebuild the string.

如果您正在寻找更具迭代性的解决方案,如果您愿意,还可以选择不使用更复杂的正则表达式。您可以拆分“并替换偶数编号的索引,然后重建字符串。

    String input = "\"unique\" unique unique \"unique\" \"unique\" \"unique\" \"unique\" unique \"unique unique\" unique unique \"";
    System.out.println(input);
    String[] split = input.split("\"");
    for (int i = 0; i < split.length; i = i + 2) {
        split[i] = split[i].replaceAll("unique", "potato");
    }
    String output = "";
    for (String s : split) {
        output += s + "\"";
    }
    System.out.println(output);

Output:

输出:

"unique" unique unique "unique" "unique" "unique" "unique" unique "unique unique" unique unique "
"unique" potato potato "unique" "unique" "unique" "unique" potato "unique unique" potato potato "

#1


6  

Search for Apple using lookaheads to make sure it is not surrounded by quotes:

使用前瞻搜索Apple以确保它不被引号括起:

(?=(([^"]*"){2})*[^"]*$)Apple

And replace by:

并替换为:

Pie

RegEx Demo

RegEx演示


Update:

更新:

Based on comments below you can use:

根据以下评论,您可以使用:

Code:

码:

String str = "Apple \"Apple\"";
String repl = str.replaceAll("(?=(([^\"]*\"){2})*[^\"]*$)Apple", "Pie");
//=> Pie "Apple" "Another Apple Apple Apple" Pie

#2


0  

I assume this is what you want:

我认为这是你想要的:

String str = "Apple \"Apple\"";
String replace = str.replaceAll("(?<!\")Apple(?!\")", "Pie");

Here is the working:https://regex101.com/r/kP0oV1/2

这是工作:https://regex101.com/r/kP0oV1/2

#3


0  

This works for your test:

这适用于您的测试:

package mavensandbox;


import static junit.framework.TestCase.assertEquals;

public class Test {

    @org.junit.Test
    public void testName() throws Exception {
        String input = "Apple(\"Apple\")";
        String output = replaceThoseWithoutQuotes("Apple", "Pie", input);
        assertEquals("Pie(\"Apple\")", output);
    }

    private String replaceThoseWithoutQuotes(String replace, String with, String input) {
        return input.replaceAll("(?<!\")" + replace + "(?!\")", with);
    }
}

I'm using what's called a negative lookahead and a negative lookbehind. It finds matches that don't have a " in front or behind of it. Does that work for you?

我正在使用所谓的负向前瞻和负面的后视。它找到没有“在它前面或后面的匹配。这对你有用吗?

#4


0  

Try matching the word with whitespace after it.

尝试将单词与后面的空格匹配。

/Apple\s/

/苹果/

Then replace with Pie with the same whitespace after it.

然后用Pie替换相同的空格。

#5


0  

There's also an option to do this without a more complicated regex if you're looking for a more iterative solution, if you're so inclined. You can split on " and replace even-numbered indexes and then rebuild the string.

如果您正在寻找更具迭代性的解决方案,如果您愿意,还可以选择不使用更复杂的正则表达式。您可以拆分“并替换偶数编号的索引,然后重建字符串。

    String input = "\"unique\" unique unique \"unique\" \"unique\" \"unique\" \"unique\" unique \"unique unique\" unique unique \"";
    System.out.println(input);
    String[] split = input.split("\"");
    for (int i = 0; i < split.length; i = i + 2) {
        split[i] = split[i].replaceAll("unique", "potato");
    }
    String output = "";
    for (String s : split) {
        output += s + "\"";
    }
    System.out.println(output);

Output:

输出:

"unique" unique unique "unique" "unique" "unique" "unique" unique "unique unique" unique unique "
"unique" potato potato "unique" "unique" "unique" "unique" potato "unique unique" potato potato "