Regex常用內容,Regex內容
常用元字元
| 代碼 |
說明 |
| . |
匹配除分行符號以外的任一字元 |
| \w |
匹配字母或數字或底線或漢字 |
| \s |
匹配任意空白字元 |
| \d |
匹配數字 |
| \b |
匹配單詞的開始或結束 |
| ^ |
匹配字串的開始 |
| $ |
匹配字串的結束 |
常用限定符
| 代碼 |
說明 |
| * |
重複零次或多次 |
| + |
重複一次或多次 |
| ? |
重複零次或一次 |
| {n} |
重複n次 |
| {n,} |
重複n次或更多次 |
| {n,m} |
重複n-m次 |
萬用字元(.)
使用"."可以匹配任何字元,如果要匹配"."本身,那麼需要使用逸出字元來取消"."的特殊意義,如"." 來匹配"."
Task Text Match cat. SuccessMatch 896. SuccessMatch ?=+. SuccessSkip abc1Pattern: ...\.
匹配特定字元
使用"[]"可以將需要匹配的特定字元放在方括弧中進行匹配,如:只匹配a,b,c,那麼可寫成[abc]
Task Text Match can SuccessMatch man SuccessMatch fan SuccessSkip dan To be completedSkip ran To be completedSkip panPattern: [cmf]an
排除特定字元
使用方法和上面的"[]"類似,只是在字元前加上"",以表示之後的這些字元是排除匹配的。如:[abc]表示除了a,b,c之外的任何字元都可以進行匹配。
Task Text Match hog SuccessMatch dog SuccessSkip bogPattern: [^b]og
字元範圍
如果要匹配的字元太多,那我們並不需要將所有字元都像上面一樣全部列出來,只需用"-"將範圍表示出來。[0-6]表示只匹配0-6的數字,[^n-p]表示不匹配n-p的任何字元。"\w"等價於[A-Za-z0-9_]
Task Text Match Ana SuccessMatch Bob SuccessMatch Cpc SuccessSkip aax To be completedSkip bby To be completedSkip cczPattern: [A-C]
重複
如果我們要匹配的相同字元有好幾個,使用"\w\w\w"就顯得有點麻煩,所以可以通過下面這種方法來匹配多個字元的情況,如:有3個"a"要匹配,則直接用a{3}表示即可。a{1,3}重複1-3次,[wxy]{5},w/x/y 任意一個字元重複5次,.{2-6}則表示任一字元重複2-6次
Task Text Match wazzzzzup SuccessMatch wazzzup SuccessSkip wazupPattern: waz{2}
"*": 重複0次或者多次 "+": 重複1次或者多次
Task Text Match aaaabcc SuccessMatch aabbbbc SuccessMatch aacc SuccessSkip aPattern: a*b*c+
"?": 重複零次或一次
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\?
匹配空白符
"\s" 可以匹配任意的空白符
Task Text Match 1. abc SuccessMatch 2. abc SuccessMatch 3. abc SuccessSkip 4.abc To be completedPattern: \d\.\s+abc
匹配行起始/結束字元
"^": ^abc 匹配以abc開頭的字元 "$": abc$ 匹配以abc結束的字元
Task Text Match Mission: successful SuccessSkip Last Mission: unsuccessful To be completedSkip Next Mission: successful upon capture of target To be completedPattern: ^Mission:
匹配組
"()" 將要匹配的內容放在()中
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+)
嵌套分組
"(())" 將多個組進行嵌套匹配
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+))
多個分組組合
Task Text Capture Groups
Capture 1280x720 1280 720 SuccessCapture 1920x1600 1920 1600 SuccessCapture 1024x768 1024 768 SuccessPattern: (\d+)\w(\d+)
增加可讀性
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?)
練習
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+))?