Java-based regular expressions

Source: Internet
Author: User
Tags alphabetic character uppercase character

The concept of regular expressions
Regular expressions, also known as formal notation, conventional notation (English: Regular expression, often abbreviated in code as regex, RegExp, or re), is a concept of computer science. A regular expression uses a single string to describe and match a series of strings that conform to a certain syntactic rule. In many text editors, regular expressions are often used to retrieve and replace text that conforms to a pattern. A regular table is used to manipulate string data.
There is a method in the String class
The public boolean matches (string regex) tells whether this string matches the given regular expression.
Parameter: Regex-Regular expression used to match this string
Returns: True if and only if this string matches the given regular expression
Introduction to Symbols:
\ \ match backslash character
\ r Match Carriage return character
\ t matches tabs
\f Match page break
\ nthe match line break
[ABC] matches one of the ABC
[^ABC] matches one in addition to ABC
[A-z] matches any character in the specified range, for example, "[A-z]" can match any lowercase alphabetic character in the range "a" through "Z".
[A-z] matches any character in the specified range, for example, "[A-z]" can match any uppercase character in the range "a" through "Z".
[A-d[m-p]] matches [a-d M-p] and sets one of the//ABCD Mnop
[A-Z&&[^BC]] matches any character in the range [Ad-z]//Take any of the A-Z but cannot take BC
[A-z&&[^m-p]] matches any character in the range of [a-l q-z]//take arbitrary but no-fetch MNOP in A-Z
[A-z&&[def]] matches any character in the Def intersection set
. 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 character, including spaces, tabs, page breaks, and so on. equivalent to [\f\n\r\t\v]
\s matches any visible character. equivalent to [^\f\n\r\t\v]
\w match Word character: equivalent to [a-za-z_0-9]
\w matches any non-word character. equivalent to [^a-za-z0-9_]
^ matches the starting position of the input string
$ matches the end position of the input string
\b Matches a word boundary
\b Matching non-word boundaries
? Match once or once without
* Match 0 or more times
+ match 1 or more times
{n} matches exactly n times
{N,} matches at least n times
{n,m} matches at least n times, but not more than M matches
\1 a back reference that represents the contents of the parentheses in the expression, from left to right, and the first opening parenthesis. \2, and so on, represents the second, and the whole expression
Use the Matches method example in the String class: match the 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: Using the Split Method example in the string class: Cutting a 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: Using the ReplaceAll Method Example in the String class: Replacing a 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,}", "#"); 4 or more than 4 numbers are joined together for a #
SYSTEM.OUT.PRINTLN (ss);
}
}
Overlapping word substitution: to replace repeated occurrences of a word, such as the AA BBB CCCC any word may be duplicated, so use. (dots) matches any character. The word is then encapsulated in a group using () parentheses. In order to reuse certain rules, the rules are encapsulated for the group. Use () \1 1 is the group number, and \1 refers to the group. is what this position is in the group. When you take a value, use the value from the first parenthesis (group), and so on.

Access: In fact, the Java.util.regex.Pattern class is used; Java.util.regex.Matcher class
Common methods in the Matcher class:
The public boolean find () tries to find the next subsequence of the input sequence that matches the pattern.
The public String group () returns the input subsequence that was matched by the previous match operation.
The public int start () returns the initial index that was previously matched.
public int End () returns the offset after the last matching character.
Example: Getting a string
public class regdemo{
public static void Main (string[] args) {
Pattern p = pattern.compile ("A*b"); Encapsulating a regular expression in an object
Matcher m = P.matcher ("Aaaaab");//Use the regular Expression method Matcher and string association to get a matching object for a string operation
Matcher Boolean B = m.matches ();//Manipulate the string by Mather the method of the Match object
System.out.println (b); Match returns True, mismatch returns false
}
}

Java-based regular expressions

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.