2016-06-27 4 views
4

사실 저는 number를 사용자가주는 문장의 단어로 대체하려고합니다. 이 사건 날짜 형식; 예를 들어 : My birthday is on 16/6/2000 and I'm newbie to the java은 ->이 될 --->My birthday is on sixteenth july two thousand and I'm newbie to the java 여기번호를 Java의 올바른 위치에있는 단어로 바꿉니다.

코드입니다 :

 Scanner reader = new Scanner(System.in); 
     System.out.println("Enter any numbers: "); 
     String nom = reader.nextLine(); // get input from user 

     //checking contains that has "/" or not 
     if(nom.contains("/")){ 
      String parts[] = nom.split("[/]"); 
      String part1 = parts[0]; //data before "/" will be stored in the first array 
      String day[] = part1.split("\\s+");// split between space 
      String get_day = day[day.length -1];// get last array 

      String get_month = parts[1]; //data in the between of "/" will be stored in the second array 

      String part3 = parts[2]; // data after "/" will be stored in the third array 
      String year[] = part3.split("\\s+");// split between space 
      String get_year = year[0];// get first array 

      String s = NumberConvert.convert(Integer.parseInt(get_day)) + 
         NumberConvert.convert(Integer.parseInt(get_month)) + 
         NumberConvert.convert(Integer.parseInt(get_year)); 

      String con = nom.replaceAll("[0-9].*/[0-9].*/[0-].*", s); // replace number to word   
      System.out.println(con); // print the data already converted 
     } else {....} 

하지만 내가 가진 한 결과는 다음과 같습니다

My birthday is on sixteenth july two thousand 

//"and I'm newbie to the java" is disappear [How to solve it]// 

방법 그것을 해결하십시오. 실제로 나는 "/"슬래시의 전후에 가치를 얻고 그것을 단어로 변환하고 그것을 사용자로부터의 원래 입력으로 대체하려고합니다.

String con = nom.replaceAll("[0-9].*/[0-9].*/[0-9999]", s); // a bit change [0-9].* to [0-9999] 

그러나 출력은 다음과 같이된다 :

내가 시도한 것은

My birthday is on sixteenth july two thousand 000 and I'm newbie to the java 
//after two thousand index "000" is appearing 
+0

숫자 나 날짜를? – aksappy

답변

3

정규식이 잘못 :

[0-9].*/[0-9].*/[0-].* 

그것은 의미 :

[0-9] match a single number in the range between 0 and 9 
.* matches any character (except newline) between zero and unlimited times, as many times as possible, giving back as needed [greedy] 
/matches the character/literally 
[0-9] match a single number in the range between 0 and 9 
.* matches any character (except newline) between zero and unlimited times, as many times as possible, giving back as needed [greedy] 
/matches the character/literally 
[0-] match a single number in the list 0- literally 
.* matches any character (except newline) between zero and unlimited times, as many times as possible, giving back as needed [greedy] 

이 있어야한다 :

[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9] 

또는 더 나은 :

또한 문자열에서 모든 숫자를 얻기 위해 정규식 패턴 아래에 사용할 수 있습니다
\d{2}/\d{2}/\d{4} 
+0

@ BackSlash 감사합니다. – Paranrax

0

:

 String st = "My birthday is on 16/6/2000 and I'm newbie to the java, using since 2015";  
     Pattern p = Pattern.compile("-?\\d+"); 
     Matcher m = p.matcher(st); 
     while (m.find()) { 
      System.out.println(m.group()); 
     } 
관련 문제