Android Regular Expressions __ Regular expressions

Source: Internet
Author: User

In the Sun's Java JDK 1.40 version, Java has its own package that supports regular expressions, and this article introduces how to use the Java.util.regex package.


To make a rough estimate, other Linu x users will encounter regular expressions except for the occasional Linux. Regular expressions are extremely powerful tools and are resilient in string pattern-matching and string pattern-substitution. In the Unix world there is little limit to regular expressions and, to be sure, it is widely used.


The engine of regular expressions has been implemented by many common UNIX tools, including Grep,awk,vi and Emacs. In addition, many of the more widely used scripting languages also support regular expressions, such as Python,tcl,javascript, and the most famous Perl.


I've been a Perl hacker a long time ago, and if you're like me, you'll be very dependent on the powerful text-munging tools you have on hand. In recent years, like other program developers, I have been paying more and more attention to Java development.


Java, as a development language, has a lot to recommend, but it has never supported regular expressions with its own. Until recently, with the help of third party libraries, Java began to support regular expressions, but these Third-party class libraries are inconsistent, poorly compatible, and poorly maintained. This shortcoming has been a huge concern for me to choose Java as the primary development tool.


You can imagine how happy it was when I knew that Sun's Java JDK 1.40 version contained Java.util.regex (a fully open, self-contained regular expression package). It's funny to say that I spent a lot of time digging through this hidden gem. I am very surprised that Java is such a great improvement (with the Java.util.regex package) Why not more public?!


Recently, Java feet have jumped into the world of regular expressions. The Java.util.regex package also has its own brilliance in support of regular expressions, and Java also provides detailed documentation of relevant instructions. Makes the dim regex mysterious scene also slowly be brushed aside. The composition of some regular expressions (perhaps most notably, the combination of character libraries) is not found in Perl.


In the Regex package, you include two classes, pattern (schema Class), and Matcher (Match Class). The pattern class is the object that is used to express and state the search patterns, and the Matcher class is the object that really affects the search. Plus a new exception class, Patternsyntaxexception, when an illegal search pattern is encountered, an exception is thrown.


Even if you're familiar with regular expressions, you'll find it fairly straightforward to use regular expressions in Java. The point is that for Perl enthusiasts who are spoiled by a single match in Perl, the use of Java regex packages for substitution operations is more cumbersome than the methods they used to.


The limitation of this article is that it is not a complete tutorial on the use of regular expressions. If the reader is to further understand the regular expression, it is recommended to read the mastering Regular Expressions of Jeffrey Frieldl, published by O ' Reilly Press. I'll give you a few examples to teach readers how to use regular expressions and how to use them more simply.


/////////////////////////////////////////////////////////////////////////////////////////


Designing a simple expression to match any phone number number can be complicated, because there are many different kinds of phone number formats. All must select a more efficient pattern. For example: (212) 555-1212, 212-555-1212 and 212 555 1212, some people would think that they are all equivalent.


Let's first make a regular expression. For simplicity, a regular expression is first identified to identify the phone number number in the following format: (NNN) nnn-nnnn.


The first step is to create a pattern object to match the substring above. Once the program is running, you can make the object generic if needed. A regular expression that matches the above format can be constructed as follows: (/d{3})/s/d{3}-/d{4}, where the/D-single character type is used to match any number from 0 to 9, and the {3} repeating symbol is a handy notation for 3 consecutive digits and is equivalent to (/D/D/D). /s also a useful single character type used to match spaces, such as space, tab, and line feed.


Isn't it easy? However, if you use the pattern of this regular expression in a Java program, there are two things to do. For the Java interpreter, the character before the backslash character (/) has a special meaning. In Java, a regex-related package does not all understand and recognize the backslash character (/), although you can try it. To avoid this, however, in order for the backslash character (/) to be completely passed in the pattern object, the double backslash character (/) should be used. In addition to the two-tier meaning of the parentheses in the regular expression, if you want it to be interpreted literally (that is, parentheses), you also need to precede it with a double backslash character (/). Which is like the following:


(//d{3}//)//s//d{3}-//d{4}


Now I'll show you how to implement the regular expression in Java code that you just talked about. The thing to remember is that in the case of a regular expression package, you need to include the package in front of the class you defined, which is the line:


Import java.util.regex.*;


The following code implements the function of reading from one line of text file to the next, and searching for phone numbers line by row, once the match is found and then output in the console.

 

1 2 3 4 5 6 7 8 9 10 11 12-13 BufferedReader in;    Pattern pattern = pattern.compile ("//(//d{3}//)//s//d{3}-//d{4}"); In
Related Article

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.