2010-08-11 6 views

답변

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

public class Test { 
    public static void main(String[] args) { 
     Pattern p = Pattern.compile("^[a-zA-Z]+([0-9]+).*"); 
     Matcher m = p.matcher("completei4e10"); 

     if (m.find()) { 
      System.out.println(m.group(1)); 
     } 

    } 
} 
+0

이것은 항상 사용하는 방법입니다. – Molske

0

는이 작업을 수행하는 여러 가지 방법이 있습니다,하지만 당신은 할 수 있습니다 :

String str = "completei4e10"; 

    str = str.replaceAll("completei(\\d+)e.*", "$1"); 

    System.out.println(str); // 4 

을 아니면 패턴은 ie 주위에있을 수 있는지에 따라 [^i]*i([^e]*)e.*입니다.

System.out.println(
     "here comes the i%@#$%@$#e there you go i000e" 
      .replaceAll("[^i]*i([^e]*)e.*", "$1") 
    ); 
    // %@#$%@$# 

[…]character class이다. [aeiou]과 같은 것은 소문자 모음 중 하나와 일치합니다. 은 이 아니며 문자 클래스입니다. [^aeiou]중 하나와 일치하지만 소문자 모음입니다.

(…)capturing group입니다. *+은이 문맥에서 repetition 지정자입니다.

관련 문제