2017-02-23 1 views
1

의 텍스트에서 특정 단어를 대체하지 않도록하는 방법 :나는이 같은 방법이 자바

for(String abc:abcs){ 
    xyz = abc.replaceAll(abc+"\\(", "_"+abc+"\\("); 
} 
나는이 시도

자바 그들을 위해 특정 접두사가 몇 교체를 교체하는 일이 없도록하는 방법

:

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

[negative lookbehind] (http://www.regular-expressions.info/lookaround.html)를 사용하십시오. –

+0

나중에 내가 시도했지만, 너무 많이 자바에, 그래서 당신이 내게 어떻게 자바에서 할 줄 알았습니까? –

답변

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 ("(?