Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Java regular expressions

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to use Java regular expressions, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

What is a regular expression?

A regular expression defines the pattern of a string. Regular expressions can be used to search, edit, or process text. Regular expressions are not limited to one language, but there are subtle differences in each language. Java regular expressions are most similar to Perl's.

The classes for Java regular expressions are in the java.util.regex package and include three classes: Pattern,Matcher and PatternSyntaxException.

The Pattern object is a compiled version of a regular expression. It doesn't have any public constructors, and we create a pattern object by passing a regular expression parameter to the public static method compile.

Matcher is a regular engine object used to match the input string and the created pattern object. This class does not have any public constructors, so we use the matcher method of the patten object and use the input string as a parameter to get a Matcher object. Then use the matches method to determine whether the input string matches the regular by the returned Boolean value.

A PatternSyntaxException exception is thrown if the regular expression syntax is incorrect.

Let's see how these classes are used in a simple example.

Package com.journaldev.util; import java.util.regex.Matcher;import java.util.regex.Pattern; public class RegexExamples {public static void main (String [] args) {/ / using pattern with flags Pattern pattern = 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 the 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 ("1x2"); 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 to provide a matches method to match pattern. The Pattern and Matcher classes are used to handle these things inside the method, but this obviously reduces the number of lines of code.

The Pattern class also has a matches method that matches the regular with the string input as a parameter, outputting a Boolean result.

The following code matches the input string with the 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 all you need is to check whether the input string matches pattern, you can save time by calling String's matches method. You only need to use Pattern and Matches classes when you need to manipulate input strings or reuse pattern.

Note that the pattern defined by the rule is applied from left to right, and once an original character is used in a match, it will not be used again.

For example, the regular "121" will only match the string "31212142121" twice, like "_ 121".

General matching symbol for regular expression

Java regular expression metacharacter

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

Add a backslash (\) before the metacharacter

Place metacharacters between\ Q (start reference) and\ E (end reference)

Regular expression quantifier

The quantifier specifies the number of times a character match occurs.

Quantifiers can be used with character classes and capturing group.

For example, [abc] + indicates that an abc b or c appears one or more times.

(abc) + indicates that the capturing group "abc" occurs one or more times. We're going to talk about 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 part of the input string that matches the capturing group will be saved in memory and can be called 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 three capturing groups; ((a) (bc)), (a) and (bc).

You can use Backreference in regular expressions, with a backslash (\) to pick up the group number you want to call.

Capturing groups and Backreferences can be confusing, so let's understand it through an example.

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 at run time is (\ w\ d), which gets "a2" and saves it in memory when it matches the input string "a2a2". So\ 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 important methods in the Pattern and Matcher classes.

We can create a Pattern object with a flag. For example, Pattern.CASE_INSENSITIVE can make case-insensitive matches. The Pattern class also provides split (String) methods similar to the String class.

The toString () method of the Pattern class returns the regular expression string compiled into this pattern.

The Matcher class has start () and end () index methods, which show the exact location matched from the input string.

The Matcher class also provides string manipulation methods replaceAll (String replacement) and replaceFirst (String replacement).

Now let's see how these functions work 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 pattern with flags Pattern pattern = 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 the 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 ("1x2"); matcher = pattern.matcher ("11234512678"); System.out.println ("Using replaceAll:" + matcher.replaceAll ("_")) System.out.println ("Using replaceFirst:" + matcher.replaceFirst ("_"));}}

The output of the above program:

Found the text "AB" starting at 0 index and ending at index 2Found the text "ab" starting at 3 index and ending at index 5Found the text "Ab" starting at 6 index and ending at index 8Split using Pattern.split (): oneSplit using Pattern.split (): twoSplit using Pattern.split (): threeSplit using Pattern.split (): fourSplit using Pattern.split (): fiveUsing replaceAll: _ 345_678Using replaceFirst: _ 34512678 is all the content of the article "how to use Java regular expressions". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report