The regular expression of Java

Source: Internet
Author: User

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");
       }
   }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.