Common Regular Expressions

Source: Internet
Author: User

Regular Expressions (understanding)
(1) A string that conforms to a certain rule
(2) Common rules
A: Character
x character X. Example: ' A ' denotes character a
\ \ backslash character.
\ n New Line (newline) character (' \u000a ')
\ r return character (' \u000d ')

B: Character class
[ABC] A, B or C (simple Class)
[^ABC] Any character except A, B, or C (negation)
[A-za-z] A to Z or A to Z, the letters at both ends are included (range)
[0-9] 0 to 9 characters are included

C: Predefined character classes
. Any character. And mine is. The character itself, how to express it? \.
\d number: [0-9]
\w Word character: [a-za-z_0-9]
Things that make up words in regular expressions must have these things.

D: Boundary Matching device
^ The beginning of the line
End of the $ line
\b Word boundaries
is the place where the word character is not.
Example: Hello World?haha;xixi

E:greedy number of words
X? X, not once or once
X* X, 0 or more times
x+ X, one or more times
X{n} X, exactly n times
X{n,} X, at least n times
X{n,m} X, at least n times, but no more than m times
(3) Common function: (who is it used separately?)
A: Judging function
Public boolean matches of the String class (string regex)
B: Split function
Public string[of the String class] split (string regex)
C: Replace function
public string ReplaceAll of the String class (String regex,string replacement)
D: Get Features
Pattern and Matcher
Pattern p = pattern.compile ("A*b");
Matcher m = P.matcher ("Aaaaab");

Find (): Lookup does not exist
Group (): Get the data you just looked up

Example one: 5 to 15 digits are required. Cannot start at 0

Enter a string from the keyboard scanner Sc=new scanner (system.in); String str=sc.nextline ();//string regex= "[1,9][0,9]{4,15}";//boolean flag=str.matches (Regex); Boolean flag= Str.matches ("^[1,9][0,9]{4,15}$");

 

Common 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.