2017-02-23 21 views
1

にテキストに特定の単語を置き換える回避する方法:私はこのようなメソッドを持っているのjava

for(String abc:abcs){ 
    xyz = abc.replaceAll(abc+"\\(", "_"+abc+"\\("); 
} 
私はこれを試してみました

Javaで彼らのために特定の接頭辞を持っているいくつかの代替品を交換しないようにする方法

String data = "Today, abc.xyz is object oriented language"; 
String regex = "(?<!abc.)xyz";  
Pattern pattern = Pattern.compile(regex); 
Matcher matcher = pattern.matcher(data); 
System.out.println(matcher.find()); 
+0

例の追加はどうですか? –

+2

[ネガティブな外観]を使用してください(http://www.regular-expressions.info/lookaround.html)。 –

+0

ya私は試しましたが、Javaではそれほど多くはありませんので、あなたはどのようにJavaでそれをやるでしょうか教えてください。 –

答えて

0

これは機能しますか?

package test; 

import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

public class Test { 
    public static void main(String[] args) { 
     String prefix = "abc"; 
     String replaceWith = " text"; 
     String input = "This xyz is an example xyz to show how you can replace certains values of the xyz.\n" 
       + "The xyz can conain any arbitrary xyz, for example abc.xyz."; 
     Pattern pattern = Pattern.compile("[^" + prefix + "].xyz"); 
     Matcher m = pattern.matcher(input); 
     while (m.find()) { 
      input = input.replace(m.group().substring(1), replaceWith); 
     } 
     System.out.println(input); 
    } 
} 
+1

私はちょうどtext.replaceAll( "(?<!abc。)" + "xyz"、 "new text")、 –

関連する問題