简体   繁体   English

替换所有字符串,但如果哈希表中存在文本,则替换为文本的第一个

[英]replace all string but first in text if present in hashmap

The text is stored in String variable and it is processed by some API to give me a hashmap which stores the keys and values. 文本存储在String变量中,并由某些API处理,从而为我提供了一个存储键和值的哈希映射。 The keys are some specific words in text and value is a new word which will replace the key in the text. 键是文本中的某些特定单词,而value是一个新单词,它将替换文本中的键。 I have to process the text such that it will replace the keys with the value from hashmap but I have to leave the first instance of the key in the text as it is. 我必须处理文本,以便它将键替换为hashmap中的值,但是我必须将键的第一个实例保留在文本中。

Problem: I am able to replace all the instances which I am doing by iterating the hashmap and replacing the keys in the text. 问题:我能够通过迭代哈希图并替换文本中的键来替换我正在执行的所有实例。 I want to leave the first matched key as it is. 我想保留第一个匹配的键。

In string functions what I see is replace, replaceAll, replaceFirst. 在字符串函数中,我看到的是replace,replaceAll,replaceFirst。

How should I handle this case. 我应该如何处理这种情况。

for eg: 例如:

input: Example [2] This is a sample text. 输入:示例[2]这是示例文本。 This is a sample text [69-3]. 这是示例文本[69-3]。 This is a sample [69-3] text. 这是示例[69-3]文本。

hashmap: {sample=sImple,text=text2,[69-3]=somenum} 哈希图:{sample = sImple,text = text2,[69-3] = somenum}

output: Example [2] This is a sample text. 输出:示例[2]这是示例文本。 This is a sImple text2 [69-3]. 这是一个简单的文本2 [69-3]。 This is a sImple text2 somenum . 这是一个简单的text2 somenum

Also the key match is for the whole word and not a subString. 同样,键匹配是针对整个单词而不是subString。 like if name is the key and surname is the string in the text then it should not be matched and sur"name" should not be changed. 例如,如果名称是关键字,而姓氏是文本中的字符串,则不应匹配,并且不应更改sur“ name”。 I m using replaceAll instead of replace to do the replacement. 我使用replaceAll而不是replace进行替换。

Thanks in advance. 提前致谢。

The following 下列

String input="Example [2] This is a sample text. This is a sample xtexty text [69-3]. This is a sample [69-3] textME text.";

        Map<String,String> map = new HashMap<String,String>();
        map.put("sample","sImple");
        map.put("text","text2");
        map.put("[69-3]","somenum");

        for(Map.Entry<String, String> entry : map.entrySet()){
            input =input.replace(entry.getKey(),entry.getValue());
        input = input.replaceFirst(entry.getValue(), entry.getKey());
        Pattern p = Pattern.compile("(\\w+)*"+entry.getValue()+"(\\w+)|(\\w+)"+entry.getValue()+"(\\w+)*");
        Matcher matcher =  p.matcher(input);
       while( matcher.find()){
          int r =  matcher.group().indexOf(entry.getValue());
          int s =r+input.indexOf(matcher.group());
     input = input.substring(0,s)+entry.getKey()+input.substring(s+entry.getValue().length());
       }    
        }

        System.out.println(input);

    }

will print: 将打印:

  Example [2] This is a sample text. This is a sImple xtexty text2 [69-3]. This is a sImple somenum textME text2.

The above piece of code will not replace substrings, works as you want . 上面的代码不会替换子字符串,可以根据需要运行。

You can find the index of the first appearance, and replace all after this index. 您可以找到第一个出现的索引,并在此索引之后替换所有索引。 Because I didn't find a replaceAll taking an offset parameter, I can suggest you this handmade solution using StringBuilder#replace : 因为我找不到带有offset参数的replaceAll ,所以我可以建议您使用StringBuilder#replace这个手工解决方案:

public static void replaceAllButFirst(StringBuilder modifiedString, String match, String replacement) {
    int index = modifiedString.indexOf(match);
    int matchLength = match.length(), replacementLength = replacement.length();
    if (index == -1) return;
    index += matchLength;
    index = modifiedString.indexOf(match, index);
    while (index != -1) {
        modifiedString.replace(index, index + matchLength, replacement);
        index += replacementLength;
        index = modifiedString.indexOf(match, index);
    }
}

Example

You can do this using regex. 您可以使用正则表达式执行此操作。

The answer to your problem was already addressed in this post: 这篇文章已经解决了您的问题的答案:

In Java how do you replace all instances of a character except the first one? 在Java中,如何替换除第一个字符以外的所有字符实例?

I kind of solved this one using replaceFirst and replaceAll. 我有点用replaceFirst和replaceAll解决了这个问题。

Create a dictionary2 which will contain Key same as in dictionary and the values in dictionary2 will be modified version of key 创建一个dictionary2,它将包含与Dictionary中相同的Key,并且dictionary2中的值将是key的修改版本

foreg: 例如:

dictionary: {sample=simple} 字典:{sample = simple}

dictionary2: {sample=sample---A--} 字典2:{sample = sample --- A--}

Then replace the first instance of the string in the text with the value in dictionary2 using replaceFirst. 然后,使用replaceFirst将文本中字符串的第一个实例替换为dictionary2中的值。

Next replaceAll the instances of the strings in the text which will leave the first instance as it is, then replace the modified first instance with the key in dictionary2. 接下来,替换文本中所有字符串实例,这些实例将使第一个实例保持原样,然后将修改后的第一个实例替换为Dictionary2中的键。

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

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