2014-01-15 4 views
0

\\d+-\\d+을이 문자열의 임의의 생성 된 번호 <!-- This is Siebel Order identified --> <tns:id> <tns:idValue>\\d+-\\d+</tns:idValue으로 바꾸고 싶습니다.정규식 문자 바꾸기

나는

String REGEXSIEBEL = "<!-- This is Siebel Order identified --> <tns:id> <tns:idValue>\\d+-\\d+</tns:idValue>"; 
java.util.regex.Pattern p1 = java.util.regex.Pattern.compile(REGEXSIEBEL); 
java.util.regex.Matcher m = p1.matcher(INPUT); 

INPUT = m.replaceAll(REGEXSIEBEL.replaceAll(String.valueOf("\\d+-\\d+"), String.valueOf(randomInt))); 

아래의 코드를 사용하고 그러나 그것은 작품을 나던.

+1

당신은 샘플 입력과 출력을 줄 수 사용해야? – nhahtdh

답변

0

당신은 http://regex101.com/를 사용하여 정규 표현식에 빠른 온라인을 확인할 수 있습니다 어쩌면 당신은 또한 더 참조 용 자바 정규 표현식에에이 튜토리얼을 확인할 수 있습니다 http://www.tutorialspoint.com/java/java_regular_expressions.htm

당신은 손에서 귀하의 작업의 전체 구현이 아래 :

String str = "<!-- This is Siebel Order identified --> <tns:id> <tns:idValue>84678468-00</tns:idValue>"; 
String patternString = "<!-- This is Siebel Order identified --> <tns:id> <tns:idValue>\\d{8}-00<\\/tns:idValue>"; 
Pattern pattern = Pattern.compile(patternString); 
Matcher matcher = pattern.matcher(str); 

StringBuffer sb = new StringBuffer(); 

while(matcher.find()){ 
    String randomNumberTag = "<!-- This is Siebel Order identified --> <tns:id> <tns:idValue>"+ 
     Integer.toString((int)(Math.random() * 99999999)) 
     +"-00</tns:idValue>"; 
    matcher.appendReplacement(sb,randomNumberTag); 
} 
matcher.appendTail(sb); 

업데이트 이전 코드는 모든 발생을 동일한 임의의 숫자로 바꾸려면 각 발생을 다른 임의의 숫자로 바꾸는 의미였습니다.

String str = "<!-- This is Siebel Order identified --> <tns:id> <tns:idValue>84678468-00</tns:idValue>"; 
String patternString = "<!-- This is Siebel Order identified --> <tns:id> <tns:idValue>\\d{8}-00<\\/tns:idValue>"; 
Pattern pattern = Pattern.compile(patternString); 
Matcher matcher = pattern.matcher(str); 

String randomNumberTag = "<!-- This is Siebel Order identified --> <tns:id> <tns:idValue>"+ 
    Integer.toString((int)(Math.random() * 99999999)) 
      +"-00</tns:idValue>"; 
System.out.println(randomNumberTag); 
if(matcher.find()){ 
    str = matcher.replaceAll(randomNumberTag); 
}