Dark Horse programmer--java Basic-Regular expression

Source: Internet
Author: User

--java Training, Android training, iOS training,. NET training look forward to sharing with you! ——

Regular Expression 1. Basic Regular Expressions

The so-called regular expression is the use of a series of predefined special characters to describe the formatting rules of a string, and then use the formatting rules to match whether a string meets the formatting requirements.

1.1. "." and "\"

“.” , in the regular expression, the bid is any one character.
"\" is an ideographic character in a regular expression, and when we need to describe a special character that has been used by a regular expression, we can convert it to its original meaning by using "\".
"\" also has some pre-defined special content in regular expressions:
\d: Represents any number
\w: denotes any single word character (only numbers, letters, underscores)
\s: Represents any white space character (\ t \ r \ \f \x0b)
\d: Represents any non-numeric character
\w: Represents any non-word character
\s: Represents any non-whitespace character

1.2. "^" and "$"

Represents a whole by adding "^" at the beginning of the regular expression and adding "$" to the end. If they are not used, the regular expression matches only parts of a string that conform to the formatting rules, but using them requires that the string must satisfy the formatting rule from beginning to end.
For example, ^\w{ 8,10 }$ a whole string can only appear with 8-10 word characters

1.3. "Character Set []"

"[]" is used to describe a single character, inside the square brackets can define the contents of this character, can also describe a range.
For example:
When we need to describe all lowercase letters Yes, we can use the range
You can also:
You can also select within multiple ranges:

1.4. "*", "+", "?" ”

Usually we need to describe the string will have a lot of repeating elements, but do not need to strictly limit the number of occurrences, we can use "*", "+" these quantifiers.
For example, an e-mail address, a number of characters are allowed to appear before the "@" character as the user name. In this case we can use "\w+" to describe at least one word character here.
"+": indicates that content can appear at least 1 consecutive times
"*": Indicates content appears 0-several times
“?” : Indicates that the content appears 0-1 times

1.5. {n}, {n,}{n,m}

In addition to the quantifiers mentioned in 3.1.4, there are times when we need to ask for specific requirements for the number of times the content appears. such as mobile phone number. The number we ask for is not a vague concept, but it must require 11 bits. Another example is that when we ask the user to enter a password, the password is 6-15 bits. When you encounter such problems, we can use:
{n}: Indicates that content must appear n times
{n,m}: Indicates content appears n-m times
{N,}: Indicates content appears at least n times
For example:
\D{11}: Indicates that the number can only appear 11 bits, which solves the above problem.

1.6. Grouping

With the above, we are not able to solve a problem like the following:
When describing a phone number, there is an area code in front of it, and the area code can be 0086 or +86
So how do we choose between the two strings?
We can then use the Grouping "()"
(): Content can be viewed as a whole
() can use the ' | ' to represent or relate. For example:
(+86|0086): This can be +86 or 0086.

2. String is a regular correlation API2.1. Matches method

The parameters of the matches () method require that we pass in a regular expression that is described by a string, and then use the string formatting rules described by the regular expression to match the current string, if satisfied then the method returns True. Otherwise, false is returned.
For example:

String emailRegEx ="^[a-zA-Z0-9_.-][email protected]([a-zA-Z0-9-]+\\.)+[a-zA-Z0-9]{2,4}$";String email ="[email protected]";System.out.println(email.matches(emailRegEx));//true
2.2. Split method

String[] split(String regex): The parameter requires passing in a regular expression described with a string, and then using the string rule described by the regular expression to match the current string and splitting the string according to the part that is satisfied.
For example:

String str ="java,c#,php,javascript";String[] array = str.split(",");//[java,c#,php,javascript]System.out.println(Arrays.toString(array));
2.3. ReplaceAll method

String replaceAll(String regex,String replacement): The parameter requires passing in a regular expression with a string description and a string that needs to be replaced, then using the string rule described by the regular expression to match the current string and replacing the satisfied part with the string that needs to be replaced.
For example:

str ="abc123bcd45ef6g7890";;strstr.replaceAll("\\d+","数字");System.out.println(str);//abc数字bcd数字ef数字g数字

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Dark Horse programmer--java Basic-Regular expression

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.