Java-based regular expressions and java Regular Expressions

Source: Internet
Author: User

Java-based regular expressions and java Regular Expressions

Regular Expression Concept
Regular Expression, also known as Regular Expression and Regular Expression (English: Regular Expression, often abbreviated as regex, regexp or RE in code), is a concept of computer science. Regular Expressions use a single string to describe and match a series of strings that conform to a certain syntax rule. In many text editors, regular expressions are usually used to retrieve and replace texts that match a certain pattern. Regular Expression is used to manipulate string data.
There is a method in the String class.
Public boolean matches (String regex) indicates whether the String matches the given regular expression.
Parameter: regex-the regular expression used to match this string
Returns true if and only if the string matches the given regular expression.
Symbol introduction:
\ Matches backslash characters
\ R match carriage return
\ T matching tabs
\ F match the newline
\ N match the linefeed
[Abc] matches
[^ Abc] matches one of
[A-z] matches any character in the specified range. For example, "[a-z]" can match any lowercase letter in the range of "a" to "z.
[A-Z] matches any character within A specified range, for example, [A-Z] can match any uppercase letter character from A to Z.
[A-d [m-p] matches one of the [a-d m-p] Union sets // abcd mnop
[A-z & [^ bc] matches any character in the range of [ad-z] // retrieves any character in a-z but cannot obtain bc
[A-z & [^ m-p] matches any character in the range of [a-l q-z] // retrieves any character in a-z but cannot obtain mnop
[A-z & [def] matches any character in the def Intersection
. Match any single character except "\ r \ n"
\ D matches a numeric character. Equivalent to [0-9]
\ D matches a non-numeric character. Equivalent to [^ 0-9]
\ S matches any invisible characters, including spaces, tabs, and page breaks. It is equivalent to [\ f \ n \ r \ t \ v]
\ S matches any visible characters. It is equivalent to [^ \ f \ n \ r \ t \ v]
\ W matching word character: equivalent to [a-zA-Z_0-9]
\ W matches any non-word characters. Equivalent to [^ A-Za-z0-9 _]
^ Match the start position of the input string
$ Match the end position of the input string
\ B matches a word boundary
\ B match non-word boundary
? Match once or once
* Match 0 times or multiple times
+ Match once or multiple times
{N} matches exactly n times
{N,} matches at least n times
{N, m} matches at least n times, but cannot exceed m times
\ 1 backward reference, indicating the content in the expression, from left to right, corresponding to the first left bracket. Similarly, \ 2 indicates the second and \ 0 indicates the entire expression.
Example of using the matches method in the String class: whether the matching mobile phone number is correct
Public class RegDemo {
Public static void main (String [] args ){
String tel = "13800001111 ";
String reg = "1 [358] \ d {9}"; // 1 [358] [0-9] {9}
Boolean B = tel. matches (reg );
System. out. println (tel + ":" + B );
}
}
Cut: Use the split method in the String class. Example: cut the String.
Public class RegDemo {
Public static void main (String [] args ){
String str1 = "wang_yu_hang ";
String str2 = "zhao fei ";
String str3 = "tang. chun. lai ";
String [] strarr1 = str1.split ("_");
String [] strarr2 = str2.split ("+ ");
String [] strarr3 = str3.split ("\\.");
For (String str: strarr1 ){
System. out. println (str );
}
}
}
Replace: Use the replaceAll method in the String class. Example: Replace the String.
Public class RegDemo {
Public static void main (String [] args ){
String str = "helloworld ";
Str = str. replaceAll ("o", "xx ");
System. out. println (str );
String ss = "tel12344556qq4564654add4646767 ";
Ss = ss. replaceAll ("\ d {4,}", "#"); // Replace four or more digits #
System. out. println (ss );
}
}
Replace overlapping words: to replace repeated words, such as aa bbb cccc, any words may be repeated, so use. (point) matches any character. then encapsulate the word into a group using brackets. to reuse some rules, the rules are encapsulated as groups. use () \ 1 is the group number, \ 1 references the group. that is, what is in the group, and what is in this position. when the value is set, use $1 to get the value in the first bracket (group), and so on.

Get: Actually, java. util. regex. Pattern class is used; java. util. regex. Matcher class
Common methods in the Matcher class:
Public boolean find () tries to find the next subsequence of the input sequence that matches the pattern.
Public String group () returns the input subsequence matched by the previous matching operation.
Public int start () returns the previously matched initial index.
Public int end () returns the Offset after the last matched character.
Example: Get a string
Public class RegDemo {
Public static void main (String [] args ){
Pattern p = Pattern. compile ("a * B"); // encapsulate the regular expression in an object.
Matcher m = p. matcher ("aaaaab"); // use the regular expression method matcher to associate with the string to obtain matching objects for string operations.
Matcher boolean B = m. matches (); // operate the string using the method of the Mather object
System. out. println (B); // returns true for matching, and false for non-matching.
}
}

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.