Common regular expression content, regular expression content

Source: Internet
Author: User

Common regular expression content, regular expression content
Common metacharacters

Code Description
. Match any character except linefeed
\ W Match letters, numbers, underscores, or Chinese Characters
\ S Match any blank characters
\ D Matching number
\ B Start or end of a matching word
^ Start of matching string
$ End of matching string
Common delimiters
Code Description
* Repeated zero or multiple times
+ Repeat once or multiple times
? Zero or one repetition
{N} Repeated n times
{N ,} Repeat n or more times
{N, m} Repeat n-m times
Wildcard (.)

You can use "." To match any character. If you want to match "." itself, you need to use escape characters to cancel the special meaning of ".", such as "." To match "."

Task    Text     Match    cat.    SuccessMatch    896.    SuccessMatch    ?=+.    SuccessSkip    abc1Pattern: ...\.

 

Match specific characters

You can use "[]" to put the specific characters to be matched in square brackets for matching. For example, if only a, B, and c are matched, the characters can be written as [abc].

Task    Text     Match    can    SuccessMatch    man    SuccessMatch    fan    SuccessSkip    dan    To be completedSkip    ran    To be completedSkip    panPattern: [cmf]an

 

Exclude specific characters

The usage method is similar to the preceding "[]", except that "" is added before the character to indicate that the matched characters are excluded. For example, [abc] indicates that any character except a, B, and c can be matched.

Task    Text     Match    hog    SuccessMatch    dog    SuccessSkip    bogPattern: [^b]og

 

Character range

If you want to match too many characters, you do not need to list all the characters as above. You only need to use "-" to represent the range. [0-6] indicates that only numbers 0-6 are matched, and [^ n-p] indicates that no character of n-p is matched. "\ W" is equivalent to [A-Za-z0-9 _]

Task    Text     Match    Ana    SuccessMatch    Bob    SuccessMatch    Cpc    SuccessSkip    aax    To be completedSkip    bby    To be completedSkip    cczPattern: [A-C]

 

Repeated

If we want to match several identical characters, using "\ w" is a little troublesome, so we can use the following method to match multiple characters, for example: if there are 3 "a" to match, use a {3. A {1, 3} repeats for 1-3 times, [wxy] {5}, w/x/y repeats for 5 times, And. {2-6} indicates that any character repeats for 2-6 times.

Task    Text     Match    wazzzzzup    SuccessMatch    wazzzup    SuccessSkip    wazupPattern: waz{2}

 

"*": Repeated 0 or multiple times "+": repeated once or multiple times

Task    Text     Match    aaaabcc    SuccessMatch    aabbbbc    SuccessMatch    aacc    SuccessSkip    aPattern: a*b*c+

 

"? ": Zero or one repeat

Task    Text     Match    1 file found?    SuccessMatch    2 files found?    SuccessMatch    24 files found?    SuccessSkip    No files found.    To be completedPattern: [0-9]+ files? found\?

 

Match blank characters

"\ S" can match any blank space character

Task    Text     Match    1.   abc    SuccessMatch    2.    abc    SuccessMatch    3.           abc    SuccessSkip    4.abc    To be completedPattern: \d\.\s+abc

 

Match the start/end characters of a row

"^": ^ Abc matches the characters starting with abc "$": abc $ matches the characters ending with abc.

Task    Text     Match    Mission: successful    SuccessSkip    Last Mission: unsuccessful    To be completedSkip    Next Mission: successful upon capture of target    To be completedPattern: ^Mission:

 

Matching Group

"()" Put the content to be matched in ()

Task    Text    Capture Groups     Capture    file_record_transcript.pdf    file_record_transcript    SuccessCapture    file_07241999.pdf    file_07241999    SuccessSkip    testfile_fake.pdf.tmp        To be completedPattern: ^(file_\w+)

 

Nested Group

"()" Nest multiple groups for matching

Task      Text    Capture Groups     Capture    Jan 1987    Jan 1987 1987    SuccessCapture    May 1969    May 1969 1969    SuccessCapture    Aug 2011    Aug 2011 2011    SuccessPattern: ^(\w+\s(\d+))

 

Multiple group combinations
Task    Text    Capture Groups
Capture 1280x720 1280 720 SuccessCapture 1920x1600 1920 1600 SuccessCapture 1024x768 1024 768 SuccessPattern: (\d+)\w(\d+)

 

Increase readability
Task    Text     Match    I love cats    SuccessMatch    I love dogs    SuccessSkip    I love logs    To be completedSkip    I love cogsPattern: I love (cats|dogs)Pattern: ([cd]ats*|[dh]ogs?)

 

Exercise
Task    Text     Match    3.14529    SuccessMatch    -255.34    SuccessMatch    128    SuccessMatch    1.9e10    SuccessMatch    123,340.00    SuccessSkip    720p    To be completedPattern: ^-?\d+(,\d+)*(\.\d+(e\d+)?)?$Capture    415-555-1234    415    SuccessCapture    650-555-2345    650    SuccessCapture    (416)555-3456    416    SuccessCapture    202 555 4567    202    SuccessCapture    4035555678    403    SuccessCapture    1 416 555 9292    416    SuccessPattern: 1?\s?\(?(\d{3})\)?[\s-]?\d{3}[\s-]?\d{4}Task    Text    Capture Groups     Capture    tom@hogwarts.com    tom    SuccessCapture    tom.riddle@hogwarts.com    tom.riddle    SuccessCapture    tom.riddle+regexone@hogwarts.com    tom.riddle    SuccessCapture    tom@hogwarts.eu.com    tom    SuccessCapture    potter@hogwarts.com    potter    SuccessCapture    harry@hogwarts.com    harry    SuccessCapture    hermione+regexone@hogwarts.com    hermione    SuccessPattern: ^([\w\.]*)Task    Text    Capture Groups     Capture    <a>This is a link</a>    a    SuccessCapture    <a href='https://regexone.com'>Link</a>    a    SuccessCapture    <div class='test_style'>Test</div>    div    SuccessCapture    <div>Hello <span>world</span></div>    div    SuccessPattern: <(\w+)*Task    Text    Capture Groups     Skip    .bash_profile        To be completedSkip    workspace.doc        To be completedCapture    img0912.jpg    img0912 jpg    SuccessCapture    updated_img0912.png    updated_img0912 png    SuccessSkip    documentation.html        To be completedCapture    favicon.gif    favicon gif    SuccessSkip    img0912.jpg.tmp        To be completedSkip    access.lock        To be completedPattern: (\w+)(\.)(jpg|png|gif)$Task    Text    Capture Groups     Capture                The quick brown fox...    The quick brown fox...    SuccessCapture       jumps over the lazy dog.    jumps over the lazy dog.    SuccessPattern: ^\s*(.*)\s*$Task    Text    Capture Groups     Skip    W/dalvikvm( 1553): threadid=1: uncaught exception        To be completedSkip    E/( 1553): FATAL EXCEPTION: main        To be completedSkip    E/( 1553): java.lang.StringIndexOutOfBoundsException        To be completedCapture    E/( 1553):   at widget.List.makeView(ListView.java:1727)    makeView ListView.java 1727    SuccessCapture    E/( 1553):   at widget.List.fillDown(ListView.java:652)    fillDown ListView.java 652    SuccessCapture    E/( 1553):   at widget.List.fillFrom(ListView.java:709)    fillFrom ListView.java 709    SuccessPattern: \.+(\w+)+\((\w+\.\w+)+:(\d+)\)Pattern: (\w+)\(([\w\.]+):(\d+)\)Task    Text    Capture Groups     Capture    ftp://file_server.com:21/top_secret/life_changing_plans.pdf    ftp file_server.com 21    SuccessCapture    https://regexone.com/lesson/introduction#section    https regexone.com    SuccessCapture    file://localhost:4040/zip_file    file localhost 4040    SuccessCapture    https://s3cur3-server.com:9999/    https s3cur3-server.com 9999    SuccessCapture    market://search/angry%20birds    market search    SuccessPattern: (\w+)://([\w\.-]+)(:(\d+))?

 

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.