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

The method of extracting characters by Java regular expression

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces "the method of extracting characters by Java regular expression". In the daily operation, I believe that many people have doubts about the method of extracting characters from Java regular expression. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "the method of extracting characters by Java regular expression". Next, please follow the editor to study!

There is a need to extract all the specific characters in the string, which is very tedious if it is handled as a regular string. So I thought of using regular expressions to do it. The project requirements are as follows: the last number in the license plate number needs to be extracted, for example: Su A7865 extract 5, Su A876X extract 6

Implementation method:

Import java.util.regex.Matcher;import java.util.regex.Pattern;public class Test {public static void main (String [] args) {String s = "A876X" / / write the string to match into a regular expression, and then enclose the characters to be extracted in parentheses / / here, we want to extract the last number, the regular rule is "a number plus 0 non-digits plus the Terminator" Pattern pattern = Pattern.compile ("(\ d) [^\ d] * $"); Matcher matcher = pattern.matcher (s) If (matcher.find ()) System.out.println (matcher.group (1));}}

About several methods in Matcher:

Mathcer.start () / Matcher.end () / Matcher.group ()

When you use matches (), lookingAt (), and find () to perform the matching operation, you can use the above three methods to get more detailed information.

Start () returns the index position of the matched substring in the string.

End () returns the index position of the last character of the matched substring in the string.

Group () returns the matching substring

Java code example:

Pattern p=Pattern.compile ("\ d +"); Matcher m=p.matcher ("aaa2223bb"); m.find (); / match 2223 m.start (); / / return 3 m.end (); / / return 7, return the index number m.group () after 2223; / / return 2223Mathcer m2=p.matcher ("2223bb"); m2.lookingAt (); / / match 2223 m2.start () / / returns 0, because lookingAt () can only match the previous string, so when using lookingAt () match, the start () method always returns 0 m2.end (); / / returns 4 m2.group (); / / returns 2223Matcher m3=p.matcher ("2223"); / / if Matcher m3=p.matcher ("2223bb"); then the following method makes an error because mismatch returns false m3.matches () / / matches the entire string m3.start (); / / returns 0 m3.end (); / / returns 3. The reason is clear, because matches () needs to match all strings m3.group (); / / returns 2223

Having said so much, I believe we all understand the use of the above methods, and we should talk about how the grouping of regular expressions is used in java.

Start (), end (), group () all have overloaded methods they are start (int I), end (int I), group (int I) are dedicated to grouping operations, and the Mathcer class also has a groupCount () to return how many groups there are.

Java code example:

Pattern p=Pattern.compile ("([a murz] +) (\ d +)"); Matcher m=p.matcher ("aaa2223bb"); m.find (); / / match aaa2223 m.groupCount (); / / returns 2 because there are two sets of m.start (1); / / returns 0 returns the index number m.start (2) of the first set of matching substrings in the string; / / returns 3 m.end (1) / / returns 3 returns the index position of the last character of the first set of matched substrings in the string. M.end (2); / / return 7 m.group (1); / / return aaa, return the first set of matched substrings m.group (2); / / return 2223, return the second set of matched substrings

Now we use a slightly more advanced regular matching operation, for example, there is a piece of text, there are a lot of numbers, and these numbers are separate, we now want to take out all the numbers in the text, using the regular operation of java is so simple.

Java code example:

Pattern p=Pattern.compile ("\ d +"); Matcher m=p.matcher ("my QQ is: 456456 my phone is: 0532214 my email is: aaa123@aaa.com"); while (m.find ()) {System.out.println (m.group ());}

Output:

456456

0532214

one hundred and twenty three

For example, replace the above while () loop with

While (m.find ()) {System.out.println (m.group ()); System.out.print ("start:" + m.start ()); System.out.println ("end:" + m.end ());}

Then output:

456456

Start:6 end:12

0532214

Start:19 end:26

one hundred and twenty three

Start:36 end:39

Now you should know that after each matching operation, the values of the three methods start (), end () and group () will change to the information of the matching substring and their overloaded methods.

Note: start (), end (), and group () can be used only when the matching operation is successful, otherwise java.lang.IllegalStateException will be thrown, that is, when any one of the matches (), lookingAt (), find () methods returns true.

At this point, the study of "the method of extracting characters from Java regular expressions" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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