Java 에서 정규식(Regex)을 사용하여 substring 하기

By | 11월 19, 2016

담에 또 써먹을 일이 있을까 모르겠지만... 일단 끄적여 본다~

 

// 이 문자열에서 시간표시 이전만 남기고 제거하고 싶다.
String msg = "블라블라블라블라 (09:01 13:57:14) 블라블라블라";
String msgSubStr = ""; // 잘라낸 결과
Pattern pattern = Pattern.compile("\\(\\d\\d"); // 위 msg 문자열에서 "(09" 에 해당하는 정규식패턴
Matcher matcher = pattern.matcher(msg);
int foundIdx = 0;
if(matcher.find()){
    foundIdx = matcher.start();
    if(foundIdx > 1){
        msgSubstr = msg.substring(0, foundIdx-1)
    }
}

 

 

 

Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
2004
5 years ago

msgSubstr = msg.split(“\\(\\d\\d”)[0];//이렇게 해도 비슷한거같기도 하네요…

itpsolver
5 years ago
Reply to  2004

의견 감사해요~