Regular Expressions (Java)

Source: Internet
Author: User
Tags character classes

Concept:

Regular expressions, also known as regular expressions. (English: Regular Expression, often abbreviated in code as regex, RegExp, or re), a concept of computer science.

Regular tables are often used to retrieve and replace text that conforms to a pattern (rule).

Use:

Typically used to judge a statement to check whether a string satisfies a format (match). String lookup, substitution, and so on.

A regular expression is a string of characters that contains some special meaning, which is called a meta-character of a regular expression.

The classes involved

Java.lang.String

Java.util.regex.Pattern----Mode

Java.util.regex.Matcher---Results

Example: "." Represents any one character. "abc" matches "..."

 Public class REGEXP {    publicstaticvoid  main (string[] args) {        // simple Introduction to Regular expressions        SYSTEM.OUT.PRINTLN ("abc". Matches ("..."));}    }

"\d"---0-9 arbitrary numbers, Java regular expressions on the basis of meta-characters need to add "\" to distinguish the escape character, so written as "\\d"

 Public classREGEXP { Public Static voidMain (string[] args) {//simple Introduction to Regular expressionsP ("ABC". Matches ("..."));//Match//"\d"---match numbersP ("d1234w". ReplaceAll ("\\d", "-"));//replace with a backslash    }     Public Static voidp (Object o) {System.out.println (o); }}

Description of the class:

Pattern

Defined:

A compiled representation of a regular expression.

A regular expression, specified as a string, must first be compiled to an instance of this class. The resulting pattern can then is used to create a Matcher object that can match arbitrary character sequences against the R Egular expression. All of the state involved in performing a match resides in the Matcher, so many matchers can share the same pattern.

A Typical invocation sequence is thus

Pattern p = pattern. compile ("A*b"); Matcher m = p. matcher ("Aaaaab"); Boolean b = M. matches ();

A matches method is defined by this class as a convenience for when a regular expression is used just once. This method compiles an expression and matches an input sequence against it in a single invocation. The statement

Boolean B = pattern.matches ("A*b", "Aaaaab");

is equivalent to the three statements above, though for repeated matches it's less efficient since it does don't allow the Compiled pattern to is reused.

The following wording is more efficient efficient, while pattern and matcher provide more methods.

Pattern p = pattern.compile ("a*b"= P.matcher ("Aaaaab"boolean b = m.matches ();

[A-z] represents a letter within a-Z range

[] Representative scope;

Qualifier modifier

? ---0 or more times

*----0 or more times

+---One or more times

{n}---occurs exactly {n} times

{n,}--appears at least n times

{n,m} appears n~m times

Range

ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern; Public classREGEXP { Public Static voidMain (string[] args) {                //RangeP ("a". Matches ("[ABC]"))); P ("A". Matches ("[^ABC]"));//except for ABC, yes.P ("A". Matches ("[A-za-z]"));//any letter can beP ("A". Matches ("[a-z]|[ A-z] "));//A-Z or a-Z, any letter can beP ("A". Matches ("[A-z[a-z]")) ;//Same as P ("A". Matches ("[A-z]&&[reg]") ;//is A-Z and one of the EEG Public Static voidp (Object o) {System.out.println (o); }}

Predefined character classes

"\ \". Matches ("\\\\")----match a backslash to write 4, the front one will be considered to be escaped, write two will be error, three escaped, four correct (temporarily unclear principle)
ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern; Public classREGEXP { Public Static voidMain (string[] args) {//Meet \s \w \dP ("\n\r\t". Matches ("\\s{4}")); P ("". Matches ("\\s")); P ("A_8". Matches ("\\w{3}")); P ("abc888&^%". Matches ("[a-z]{1,3}\\d+[&^#%]+")); P ("\ \". Matches ("\\\\"))); }     Public Static voidp (Object o) {System.out.println (o); }}
Predefined character classes
. Any character (could or may isn't match line terminators)
\d A Digit: [0-9]
\d A non-digit: [^0-9]
\h A Horizontal whitespace character: [\t\xa0\u1680\u180e\u2000-\u200a\u202f\u205f\u3000]
\h A non-horizontal whitespace character: [^\h]
\s A whitespace character: [\t\n\x0b\f\r]
\s A non-whitespace character: [^\s]
\v A vertical whitespace character: [\n\x0b\f\r\x85\u2028\u2029]
\v A non-vertical whitespace character: [^\v]
\w A Word character: [a-za-z_0-9]
\w A Non-word character: [^\w]

Find ()

Attempts to find the next subsequence (sub-sequence) of the of the input sequence that matches the pattern.

Reset ()

Resetting a matcher discards all of their explicit state information and sets their append position to zero.

ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern; Public classREGEXP { Public Static voidMain (string[] args) {                //matches find lookingPattern p = pattern.compile ("\\d{3,5}"); String s= "123-45623-789-00"; Matcher m=P.matcher (s);        P (m.matches ());        M.reset (); The//matches method and the Find method will cause conflicts, remember to call the Reset method P (M.find ()); P (M.start ()+"-"+m.end ());        P (M.find ()); P (M.start ()+"-"+m.end ());        P (M.find ()); P (M.start ()+"-"+m.end ());        P (M.lookingat ());        P (M.lookingat ());        P (M.lookingat ());                    P (M.lookingat ()); }     Public Static voidp (Object o) {System.out.println (o); }}

Find an alternative

ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern; Public classREGEXP { Public Static voidMain (string[] args) {//replacement can refer to the description of Appendreplacement () in the API documentationPattern p = pattern.compile ("java", pattern.case_insensitive); Matcher m= P.matcher ("Java java java I love java u hate java sfarwwfr"); //p (M.replaceall ("JAVA"));//all replaced by JavaStringBuffer buf =NewStringBuffer (); inti = 0;  while(M.find ()) {//Looking fori++; if(i%2 = = 0) {//Replace the singular with the JavaM.appendreplacement (buf, "Java"); } Else{m.appendreplacement (buf,"JAVA"); }} m.appendtail (BUF);//appendreplacement () fill the tail with this method after multiple callsp (BUF); }     Public Static voidp (Object o) {System.out.println (o); }}

Group

Matcher.group ()-----Returns the input subsequence matched by the previous match.

1 ((A) (B (C)))
2 (A)
3 (B (C))
4 (C)

Group uses parentheses to get different groupings, Eg:group (1); Group (2)

 Public classREGEXP { Public Static voidMain (string[] args) {//GroupregexPattern p = pattern.compile ("(\\d{3,5}) | ( [A-z] {2}) "); String s= "123aa-34345bb-234cc-00"; Matcher m=P.matcher (s);  while(M.find ()) {p (M.group (2)); }    }     Public Static voidp (Object o) {System.out.println (o); }}

Summarize several important points of knowledge:

Regular Expressions (Java)

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.