Java Learning Note 06--Regular expressions

Source: Internet
Author: User

Java Learning Note 06--Regular expressions

Regular expressions can easily match data and perform more complex string validation, splitting, and substitution operations.

For example, to determine whether a string consists of numbers, there are two ways to do this:

Do not use regular expressions

[Java]View Plaincopy
  1. Public class T {
  2. public static void Main (string[] args) {
  3. String str = "0123456789";
  4. Boolean flag = true; //define a tag variable
  5. char c[] = Str.tochararray ();
  6. For (int i = 0; i < c.length; i++) {
  7. if (c[i]<' 0 ' | | c[i]>' 9 ') {
  8. Flag = false;
  9. Break ;
  10. }
  11. }
  12. if (flag) {
  13. System.out.println ("is all made up of numbers");
  14. }else{
  15. System.out.println ("Not all of them are made up of numbers");
  16. }
  17. }
  18. }

Using regular expressions

[Java]View Plaincopy
  1. Public class T {
  2. public static void Main (string[] args) {
  3. String str = "0123456789";
  4. if (Pattern.compile ("[0-9]+"). Matcher (str). Matches ()) {
  5. System.out.println ("is all made up of numbers");
  6. }else{
  7. System.out.println ("Not all of them are made up of numbers");
  8. }
  9. }
  10. }


Pattern, Matcher class

These two classes are the core operation classes of a regular. All two of these classes are defined in the Java.util.regex package.

The main function of the pattern class is to make the regular specification (as in the previous "0-9", which belongs to the regular specification), while the Matcher class is primarily the execution specification, verifying that a string conforms to its specification.

\d: Indicates a number, "0-9"

\d: Denotes non-numeric, "^0-9"

\w: denotes letters, numbers, underscores, "a-za-z0-9"

\w: "^a-za-z0-9"

Above, if you want to drive, you must rely on the pattern class and the Matcher class.

Pattern is mainly about the meaning of a rule: regular expression rules need to be used in the pattern class.

The Matcher class mainly represents a validation rule that is specified using pattern.

In the pattern class, if you want to get an instance of the pattern class, you must call the compile () method.

Example: Verify that a string is a valid date format.

[Java]View Plaincopy
  1. Public class T {
  2. public static void Main (string[] args) {
  3. String str = "1990-04-13";
  4. String pattern = "\\d{4}-\\d{2}-\\d{2}";
  5. Pattern P = pattern.compile (pattern); //Instantiate pattern class
  6. Matcher m = p.matcher (str); //instantiation of the Matcher class
  7. if (m.matches ()) {
  8. System.out.println ("is a valid date format");
  9. }else{
  10. System.out.println ("not valid date format");
  11. }
  12. }
  13. }


You can also use the regular string splitting function in the pattern class.

[Java]View Plaincopy
  1. Public class T {
  2. public static void Main (string[] args) {
  3. String str = "AAA000BBB111CCC";
  4. String pattern = "\\d+";
  5. Pattern P = pattern.compile (pattern);
  6. String s[] = p.split (str);
  7. For (int i = 0; i < s.length; i++) {
  8. System.out.println (S[i]);
  9. }
  10. }
  11. }

You can also use the string substitution feature in the Matcher class.

[Java]View Plaincopy
  1. Public class T {
  2. public static void Main (string[] args) {
  3. String str = "AAA000BBB111CCC";
  4. String pattern = "\\d+";
  5. Pattern P = pattern.compile (pattern);
  6. Matcher m = p.matcher (str);
  7. String s = m.replaceall ("---");
  8. System.out.println (s); //AAA---BBB---CCC
  9. }
  10. }


String class support for regular

From the previous operation, can be returned, a lot of code in addition to the required strings, the use of different regular rules, basically there is nothing special.

So after JDK1.4, Java has some extensions to the regular, starting with the direct support for regular operations in string.

[Java]View Plaincopy
  1. Public class T {
  2. public static void Main (string[] args) {
  3. String str1 = "AAA000BBB";
  4. String str2 ="1990-04-13";
  5. String STR3 = "I_love_you";
  6. System.out.println ("string substitution:" +str1.replaceall ("\\d+", "---"));
  7. System.out.println ("string validation:" +str2.matches ("\\d{4}-\\d{2}-\\d{2}"));
  8. String s[] = Str3.split ("_");
  9. System.out.println ("string split:");
  10. For (int i=0;i<s.length;i++) {
  11. System.out.println (S[i]);
  12. }
  13. }
  14. }


Summarize:

1, the use of the regular can be easily completed string verification, splitting, replacement and other complex functions.

2, in the development of the general will be directly using the string class to provide good regular support, and often rarely directly use the pattern class or Matcher class.

3, in some of the regular application, for some sensitive characters to be escaped operation.

Java Learning Note 06--Regular expressions

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.