简体   繁体   English

使用正则表达式 java 字符串查找和替换字符

[英]find and replace characters using regexp java string

I want to replace special characters between the following String when only when they are between two words, BEGIN and END .我想替换以下字符串之间的特殊字符,仅当它们位于两个单词 BEGIN 和 END 之间时。 xxx to CCC and yyy to DDD, using Java regular expression or some way ? xxx 到 CCC 和 yyy 到 DDD,使用 Java 正则表达式或某种方式? can you help你能帮我吗


Raw string = "John Doe xxx Amazing man BEGIN reference xxx yes yyy indeed this is true xxx 
no yyy END , so this xxx does not change" 


converted String = "John Doe xxx Amazing man BEGIN reference CCC yes DDD indeed this is true 
CCC no DDD END, so this xxx does not change" 

You need to define your pattern and matcher:您需要定义您的模式和匹配器:

Pattern MY_PATTERN = Pattern.compile("BEGIN" + "([ ]*+[0-9A-Za-z]++[ ]*+)*" + "xxx" + "([ ]*+[0-9A-Za-z]++[ ]*+)*" + "END");
Matcher m = MY_PATTERN.matcher(rawString);

Then you will call the find method on the matcher and every time it finds what you wanted will replace it with what you needed:然后你将在匹配器上调用 find 方法,每次它找到你想要的东西时都会用你需要的东西替换它:

 while (m.find()) {
        rawString = rawString.replaceFirst(m.group(0),m.group(0).replaceAll("xxx","CCC"));
    }

Even though your sample data displays "xxx" before "yyy" , there's no specification if "xxx" is before "yyy" or vice versa.即使您的示例数据在"yyy" "xxx"之前显示"xxx" ,也没有规范是"xxx""yyy"之前,反之亦然。 You just state, "special characters between the following String when only when they are between two words, BEGIN and END" .您只需声明"special characters between the following String when only when they are between two words, BEGIN and END"

I see having a Map<String, String> where the keys are your "special" strings and the values are the strings that'll replace the "special" strings.我看到有一个Map<String, String> ,其中键是您的“特殊”字符串,值是将替换“特殊”字符串的字符串。

Iterate through this map and provide the keys to this:遍历此映射并提供其密钥:

String.format("BEGIN.*?(%s).*?END", kvp.getKey())

In this example, it'll produce two regex patterns:在这个例子中,它将产生两个正则表达式模式:

"BEGIN.*?(xxx).*?END"
"BEGIN.*?(yyy).*?END"

This will capture your "special" strings into capture group 1, which you'll provide to String.replace() like so:这会将您的“特殊”字符串捕获到捕获组 1 中,您将像这样将其提供给String.replace()

raw = raw.replace(matcher.group(), matcher.group().replace(matcher.group(1), kvp.getValue()));

matcher.group() is the entire matching string BEGIN ... END and matcher.group(1) will either be xxx or yyy matcher.group()是整个匹配字符串BEGIN ... ENDmatcher.group(1)将是xxxyyy

Put this all together and you have:把这一切放在一起,你有:

public static void main(String[] args) throws Exception {
    Map<String, String> replacerMap = new HashMap() {{
        put("xxx", "CCC");
        put("yyy", "DDD");
    }};

    String raw = "John Doe xxx Amazing man BEGIN reference xxx yes yyy indeed this is true xxx no yyy END , so this xxx does not change";
    System.out.println("Before: ");
    System.out.println(raw);
    System.out.println();

    for (Map.Entry<String, String> kvp : replacerMap.entrySet()) {
        Matcher matcher = Pattern.compile(String.format("BEGIN.*?(%s).*?END", kvp.getKey())).matcher(raw);
        if (matcher.find()) {
            raw = raw.replace(matcher.group(), matcher.group().replace(matcher.group(1), kvp.getValue()));
        }
    }

    System.out.println("After: ");
    System.out.println(raw);
}

Results:结果:

Before: 
John Doe xxx Amazing man BEGIN reference xxx yes yyy indeed this is true xxx no yyy END , so this xxx does not change

After: 
John Doe xxx Amazing man BEGIN reference CCC yes DDD indeed this is true CCC no DDD END , so this xxx does not change

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM