2013-04-16 7 views
0

일치하는 부분을 대체하고 싶습니다.Java 대체 Matcher

예 : 반환 \b\w\w\b 정규식과 일치하는 것 isat를 교체 일치 얼마나

this <h1>is</h1> awesome look <h1>at</h1> this two letter words get replaced 

공지 될 것

this is awesome look at this two letter words get replaced 

.

이것은 내가 작업하고있는 코드입니다. 그것은 끝나지 않았습니다. 그러나 나는 조금 혼란스럽고 더 쉬운 방법이 있는지 궁금합니다. 문자열을 찾고 성냥을 찾는 중이 었어. 그런 다음 ArrayList에 추가하고 각각을 대체했습니다. 문제는 내가이 {를 대체하고있는 것 중 하나이며 나는 그들을 계속 추가하기 때문에이 지속적으로 브래킷을 교체하는 모습 지금 {{}

로 교체 할 ... 그래서 내 다른 생각에 있었다 그것들을 모두 하나의 char로 대체하고 새로운 StringBuilder 객체에 추가 하시겠습니까?

ArrayList<String> replacements = new ArrayList<String>(); 

String s = "::"; 

s += command; 

Pattern p = Pattern.compile("[!-~]"); 
Matcher match = p.matcher(this.execution); 

while(match.find()) 
{ 
    replacements.add(match.group()); 
} 

StringBuilder string = new StringBuilder(); 

for(int i=0; i<this.execution.length(); i++) 
{ 
    String a =new String(execution.charAt(i)); 
    if() 
    { 
    } 
} 
s += "::" + this.execution; 

답변

1
정말 코드가 위에서 설명 된 요구 사항을 해결할 수있는 방법을 얻을하지 않습니다

..., 그것을 사용하여 작업의 종류를 할 수있는 쉬운 방법이 될 것으로 보인다 말했다

자바의 일반적으로 두 글자 단어에 대한 replaceAll 방법 :

"this is awesome look at this two letter words get replaced" 
.replaceAll("(\\b\\w{2}\\b)", "<h1>$1</h1>"); 

이 인쇄 :

this <h1>is</h1> awesome look <h1>at</h1> this two letter words get replaced 
+0

참은 아닙니다이었다 똑똑한 움직임. 나는 다른 것을 위해 그것을 쓰고 있었다 tho. 각 숯이 어떤 숯이 아니 었는지 확인하고 나서 그것을 교체 할 필요가있었습니다. 그래서 그것을 다시 구축하는 동안 문자열 작성기에 추가했습니다. –