Find the longest consecutive number string in a string (regular expression)

Topic description

Read in a string str and output the longest continuous number string in the string str

Enter description:

Each test input contains 1 test case, a string str, and the length does not exceed 255.

Output description:

Outputs the longest consecutive string of digits in str in one line.
Example 1

enter

abcd12345ed125ss123456789

output

123456789
1  /** 
2  * 
 3  * 
 4  Replace non-numeric characters with regular expressions and split to get a string array
 5  * @author Dell
 6  *
 7   */ 
8  import java.util.Scanner;
 9  public  class Main {
 10      public  static  void main(String[] args) {
 11 Scanner sc = new Scanner(System.in);
 12 String str = sc.nextLine();
 13  // Replace non-numeric characters with regular expressions and split them to get strings array 
14 String[] ss = str.replaceAll("[^0-9]",",").split(",");
15 //遍历 找出最大的
16 int maxSize = 0;
17 String res = "";
18 for (int i = 0; i < ss.length; i++) {
19     if (ss[i].length()>maxSize) {
20         maxSize = ss[i].length();
21         res = ss[i];
22     }
23 }
24     System.out.println(res);
25 }
26 }

 



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325156762&siteId=291194637