Do not use regular expressions:
Import Java.util.Scanner;
public class RagexDemo01 {
public static void Main (string[] args) {
Scanner Scanner = new Scanner (system.in);
String str = Scanner.next ();
Boolean flag = true;
Char[] C = Str.tochararray ();
for (int i = 0; i < c.length; i++) {
if (C[i] < ' 0 ' | | c[i] > ' 9 ') {
Flag = false;
System.out.println ("fffffffff");
Break
}
}
if (flag) {
System.out.println ("All are number!");
} else {
System.out.println ("Not all are number!");
}
}
}
Use regular Expressions:
Import Java.util.Scanner;
Import Java.util.regex.Pattern;
public class RegexDemo02 {
public static void Main (string[] args) {
Scanner Scanner = new Scanner (system.in);
String string = Scanner.next ();
if (Pattern.compile ("[0-9]+"). Matcher (String). Matches ()) {
System.out.println ("All are num!");
} else {
System.out.println ("Not all are num!");
}
}
}
If you want to apply regular expressions in your program, you must rely on the pattern class and the Matcher class, which are defined in the Java.util.regex package. Two classes. The main function of the pattern class is to write the regular specification, and the Matcher class is the execution specification, which verifies whether a string conforms to its specification.
Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;
public class RegexDemo03 {
public static void Main (string[] args) {
String str = "19w3-12-51";
String Pat = "\\d{4}-\\d{2}-\\d{2}";
Pattern p = pattern.compile (PAT);
Matcher m = p.matcher (str);
if (M.matches ()) {
System.out.println ("okkkkkk!");
} else {
System.out.println ("Noooooo!");
}
}
}
To split a string by a number:
public class RegexDemo04 {
public static void Main (string[] args) {
String str = "a1bfoeiwoef02223iofejw09323";
String Pat = \\d; \d Representative Numbers
Pattern p = pattern.compile (PAT);
String s[] = p.split (str);
for (int x = 0; x < s.length; × x + +) {
System.out.println (s[x] + "\ t");
}
}
}
Replace all numbers as #:
Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;
public class RegexDemo05 {
public static void Main (string[] args) {
String str = "A1B32GEJO31BFEO231NFE";
String Pat = "\\d+";
Pattern p = pattern.compile (PAT);
Matcher m = p.matcher (str);
String newstring = M.replaceall ("#");
System.out.println (newstring);
}
}
A method that supports regular expressions using the String class directly:
Public class RegexDemo06 {
public static void Main (string[] args) {
; String str1 = "a3ofe21fafeo23faf321". ReplaceAll ("\\d", "#");
Boolean temp = "1932-32-12". Matches ("\\d{4}-\\d{2}-\\d{2}");
String s[] = "A1l309lfe233202fag21lf". Split ("\\d+");
System.out.println (STR1);
System.out.println (temp);
for (int x = 0; x < s.length, x + +) {
System.out.println (s[x] + "\ t");
}
}
}