2016-06-27 12 views
4

実際に私はnumberをユーザーが与える文の中の単語に置き換えようとしています。このケースの日付形式。例えば: - :番号を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]// 

どのように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

がコードであるになりますそれを解決する。実際には "/"スラッシュの前と後に値を取得し、それを単語に変換し、それを元の入力としてユーザーから置き換えたいと考えています。

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()); 
     } 
関連する問題