Java Regular Expression Learning Tutorial _ regular expressions

Source: Internet
Author: User
Tags character classes

This tutorial is designed to help you navigate Java regular expressions while also helping me review regular expressions.

What is a regular expression?

The regular expression defines the pattern of the string. Regular expressions can be used to search, edit, or work with text. Regular expressions are not limited to a single language, but have subtle differences in each language. Java regular expressions are the most similar to Perl.

The classes of Java regular expressions are in the Java.util.regex package, including three classes:pattern,matcher and patternsyntaxexception.

The pattern object is a compiled version of a regular expression. He doesn't have any public constructors, we create a pattern object by passing a regular expression argument to the public static method compile.
Matcher is the regular engine object used to match the input string and the pattern object created. This class does not have any public constructors, and we use the Matcher method of the Patten object to obtain a Matcher object using the input string as a parameter. The matches method is then used to determine whether the input string matches the regular by returning a Boolean value.
If the regular expression syntax is incorrect, the patternsyntaxexception exception is thrown.
Let's look at a simple example of how these classes work.

Package com.journaldev.util;
Import Java.util.regex.Matcher;
 
Import Java.util.regex.Pattern; public class Regexexamples {public static void main (string[] args) {//using mode with the flags pattern Patt
    Ern = Pattern.compile ("ab", pattern.case_insensitive);
    Matcher Matcher = Pattern.matcher ("Abcabdab"); Using Matcher find (), group (), Start () and End () methods while (Matcher.find ()) {System.out.println ("Found t He text \ "" + matcher.group () + "\ Starting at" + Matcher.start () + "index and ending at index" +
    Matcher.end ());
    //using pattern split () method pattern = Pattern.compile ("\\w");
    string[] Words = Pattern.split ("one@two#three:four$five");
    for (String s:words) {System.out.println ("Split using Pattern.split ():" + s);
    }//Using Matcher.replacefirst () and ReplaceAll () Methods pattern = Pattern.compile ("1*2");
    Matcher = Pattern.matcher ("11234512678"); System.out.println ("Using replaceall:" + Matcher.replaceall ("_"));
  System.out.println ("Using replacefirst:" + Matcher.replacefirst ("_")); }
 
}

Since regular expressions are always related to strings, Java 1.4 extends the string class, providing a matches method to match pattern. Use pattern and Matcher classes to handle these things internally, but obviously this reduces the number of lines in the code.

The pattern class also has a matches method that matches the regular to the string entered as a parameter and outputs a Boolean result.

The following code can match an input string with a regular expression.

String str = "BBB";
System.out.println ("Using String matches Method:" +str.matches (". BB"));
System.out.println ("Using pattern matches Method:" +pattern.matches (". BB", str));

So if your need is simply to check if the input string matches the pattern, you can save time by calling the string's matches method. You need to use pattern and matches classes only if you need to manipulate the input string or reuse pattern.

Note that regular-defined pattern is applied from left to right, and once a primitive character has been used in a match, it will not be used again.

For example, regular "121" matches only two times string "31212142121″, just like this" _121____121″.
Regular expression generic match symbol

Java regular expression meta-characters

There are two ways to use meta characters in regular expressions like normal characters.

Add a backslash (\) before a metacharacters
Place metacharacters between \q (start reference) and \e (end reference)
Regular Expression quantifiers

Quantifiers Specify the number of occurrences of a character match.

Quantifiers can be used with character classes and capturing group.

For example, [abc]+ indicates that a,b or C occurs one or more times.

(ABC) + indicates that the capturing group "ABC" appears one or more times. We are going to discuss capturing group.

Regular expression capturing group

Capturing group is used to deal with multiple characters that appear as a whole. You can create a group by using (). The parts of the input string that match the capturing group are saved in memory and can be invoked by using backreference.

You can use the Matcher.groupcount method to get the number of capturing groups in a regular pattern. For example ((a) (BC)) contains 3 capturing groups; ((a) (BC)), (a) and (BC).

You can use backreference in regular expressions, a backslash (\) to call the group number.

Capturing groups and backreferences may be confusing, so let's take an example to understand.

System.out.println (Pattern.matches ("(\\w\\d) \\1", "a2a2")); True
  System.out.println (pattern.matches (\\w\\d) \\1, a2b2));//false
  System.out.println ( Pattern.matches ("(AB) (b\\d) \\2\\1", "Abb2b2ab")); True
  System.out.println (Pattern.matches ("(AB) (b\\d) \\2\\1", "Abb2b3ab"));//false

In the first example, the first capturing group is (\w\d), which gets the A2″ and saves it in memory when it matches the input string "A2a2″." Therefore \1 is a reference to "A2" and returns True. For the same reason, the second line of code prints false.

Try to understand the third and fourth lines of code yourself. :)

Now let's look at some of the important methods in pattern and Matcher classes.

We can create a pattern object with a logo. For example, pattern.case_insensitive can be case insensitive to match. The pattern class also provides a split (string) method similar to the String class

The Pattern class ToString () method returns a regular expression string that is compiled into this pattern.

The Matcher class has the start () and End () indexing methods, and they can display the exact location from the input string.

The Matcher class also provides string manipulation methods ReplaceAll (string replacement) and Replacefirst (string replacement).

Now let's look at how these functions are used in a simple Java class.

Package com.journaldev.util;
Import Java.util.regex.Matcher;
 
Import Java.util.regex.Pattern; public class Regexexamples {public static void main (string[] args) {//using mode with the flags pattern Patt
    Ern = Pattern.compile ("ab", pattern.case_insensitive);
    Matcher Matcher = Pattern.matcher ("Abcabdab"); Using Matcher find (), group (), Start () and End () methods while (Matcher.find ()) {System.out.println ("Found t He text \ "" + matcher.group () + "\ Starting at" + Matcher.start () + "index and ending at index" +
    Matcher.end ());
    //using pattern split () method pattern = Pattern.compile ("\\w");
    string[] Words = Pattern.split ("one@two#three:four$five");
    for (String s:words) {System.out.println ("Split using Pattern.split ():" + s);
    }//Using Matcher.replacefirst () and ReplaceAll () Methods pattern = Pattern.compile ("1*2");
    Matcher = Pattern.matcher ("11234512678"); System.out.println ("Using replaceall:" + Matcher.replaceall ("_"));
  System.out.println ("Using replacefirst:" + Matcher.replacefirst ("_"));
 }
 
}

Output of the above program:

Found the text "AB" starting at 0 index and ending at index 2
Found the text "AB" starting at 3 index and ending at Dex 5
Found the text "Ab" starting at 6 index and ending at index 8
split using Pattern.split (): One
split usi Ng Pattern.split (): Two
split using Pattern.split (): Three
split using Pattern.split (): Four
split using Pattern.split (): Five
using ReplaceAll: _345_678
using Replacefirst: _34512678

This is not a very comprehensive Java regular Expression Learning course, I hope to help you learn.

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.