正则表达式常用内容
常用元字符
.匹配除换行符以外的任意字符\w匹配字母或数字或下划线或汉字\s匹配任意空白字符\d匹配数字\b匹配单词的开始或结束^匹配字符串的开始$匹配字符串的结束常用限定符
*重复零次或多次+重复一次或多次?重复零次或一次{n}重复n次{n,}重复n次或更多次{n,m}重复n-m次通配符(.)
使用"."可以匹配任何字符,如果要匹配"."本身,那么需要使用转义字符来取消"."的特殊意义,如"." 来匹配"."
Task Text Match cat. Success Match 896. Success Match ?=+. Success Skip abc1 Pattern: ...\.
匹配特定字符
使用"[]"可以将需要匹配的特定字符放在方括号中进行匹配,如:只匹配a,b,c,那么可写成[abc]
Task Text Match can Success Match man Success Match fan Success Skip dan To be completed Skip ran To be completed Skip pan Pattern: [cmf]an
排除特定字符
使用方法和上面的"[]"类似,只是在字符前加上"",以表示之后的这些字符是排除匹配的。如:[abc]表示除了a,b,c之外的任何字符都可以进行匹配。
Task Text Match hog Success Match dog Success Skip bog Pattern: [^b]og
字符范围
如果要匹配的字符太多,那我们并不需要将所有字符都像上面一样全部列出来,只需用"-"将范围表示出来。[0-6]表示只匹配0-6的数字,[^n-p]表示不匹配n-p的任何字符。"\w"等价于[A-Za-z0-9_]
Task Text Match Ana Success Match Bob Success Match Cpc Success Skip aax To be completed Skip bby To be completed Skip ccz Pattern: [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 Success Match wazzzup Success Skip wazup Pattern: waz{2}
"*": 重复0次或者多次 "+": 重复1次或者多次
Task Text Match aaaabcc Success Match aabbbbc Success Match aacc Success Skip a Pattern: a*b*c+
"?": 重复零次或一次
Task Text Match 1 file found? Success Match 2 files found? Success Match 24 files found? Success Skip No files found. To be completed Pattern: [0-9]+ files? found\?
匹配空白符
"\s" 可以匹配任意的空白符
Task Text Match 1. abc Success Match 2. abc Success Match 3. abc Success Skip 4.abc To be completed Pattern: \d\.\s+abc
匹配行起始/结束字符
"^": ^abc 匹配以abc开头的字符 "$": abc$ 匹配以abc结束的字符
Task Text Match Mission: successful Success Skip Last Mission: unsuccessful To be completed Skip Next Mission: successful upon capture of target To be completed Pattern: ^Mission:
匹配组
"()" 将要匹配的内容放在()中
Task Text Capture Groups Capture file_record_transcript.pdf file_record_transcript Success Capture file_07241999.pdf file_07241999 Success Skip testfile_fake.pdf.tmp To be completed Pattern: ^(file_\w+)
嵌套分组
"(())" 将多个组进行嵌套匹配
Task Text Capture Groups Capture Jan 1987 Jan 1987 1987 Success Capture May 1969 May 1969 1969 Success Capture Aug 2011 Aug 2011 2011 Success Pattern: ^(\w+\s(\d+))
多个分组组合
Task Text Capture GroupsCapture 1280x720 1280 720 Success Capture 1920x1600 1920 1600 Success Capture 1024x768 1024 768 Success Pattern: (\d+)\w(\d+)
增加可读性
Task Text Match I love cats Success Match I love dogs Success Skip I love logs To be completed Skip I love cogs Pattern: I love (cats|dogs) Pattern: ([cd]ats*|[dh]ogs?)
练习
Task Text Match 3.14529 Success Match -255.34 Success Match 128 Success Match 1.9e10 Success Match 123,340.00 Success Skip 720p To be completed Pattern: ^-?\d+(,\d+)*(\.\d+(e\d+)?)?$ Capture 415-555-1234 415 Success Capture 650-555-2345 650 Success Capture (416)555-3456 416 Success Capture 202 555 4567 202 Success Capture 4035555678 403 Success Capture 1 416 555 9292 416 Success Pattern: 1?\s?\(?(\d{3})\)?[\s-]?\d{3}[\s-]?\d{4} Task Text Capture Groups Capture [email protected] tom Success Capture [email protected] tom.riddle Success Capture [email protected] tom.riddle Success Capture [email protected] tom Success Capture [email protected] potter Success Capture [email protected] harry Success Capture [email protected] hermione Success Pattern: ^([\w\.]*) Task Text Capture Groups Capture <a>This is a link</a> a Success Capture <a href='https://regexone.com'>Link</a> a Success Capture <div class='test_style'>Test</div> div Success Capture <div>Hello <span>world</div> div Success Pattern: <(\w+)* Task Text Capture Groups Skip .bash_profile To be completed Skip workspace.doc To be completed Capture img0912.jpg img0912 jpg Success Capture updated_img0912.png updated_img0912 png Success Skip documentation.html To be completed Capture favicon.gif favicon gif Success Skip img0912.jpg.tmp To be completed Skip access.lock To be completed Pattern: (\w+)(\.)(jpg|png|gif)$ Task Text Capture Groups Capture The quick brown fox... The quick brown fox... Success Capture jumps over the lazy dog. jumps over the lazy dog. Success Pattern: ^\s*(.*)\s*$ Task Text Capture Groups Skip W/dalvikvm( 1553): threadid=1: uncaught exception To be completed Skip E/( 1553): FATAL EXCEPTION: main To be completed Skip E/( 1553): java.lang.StringIndexOutOfBoundsException To be completed Capture E/( 1553): at widget.List.makeView(ListView.java:1727) makeView ListView.java 1727 Success Capture E/( 1553): at widget.List.fillDown(ListView.java:652) fillDown ListView.java 652 Success Capture E/( 1553): at widget.List.fillFrom(ListView.java:709) fillFrom ListView.java 709 Success Pattern: \.+(\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 Success Capture https://regexone.com/lesson/introduction#section https regexone.com Success Capture file://localhost:4040/zip_file file localhost 4040 Success Capture https://s3cur3-server.com:9999/ https s3cur3-server.com 9999 Success Capture market://search/angry%20birds market search Success Pattern: (\w+)://([\w\.-]+)(:(\d+))?
相关推荐
wangzhaotongalex 2020-10-20
rechanel 2020-11-16
cshanzhizi 2020-10-16
luofuIT成长记录 2020-09-22
taomengxing 2020-09-07
MaggieRose 2020-08-19
jyj00 2020-08-15
MaggieRose 2020-07-04
modaiairen 2020-06-28
ziggurat 2020-06-28
JnX 2020-06-27
jyj00 2020-06-26
山水沐光 2020-06-25
shqhope 2020-06-23
eroshn 2020-06-21
码墨 2020-06-16
wyq 2020-11-11
TLROJE 2020-10-26
风雨断肠人 2020-10-13