RegexOne : 정규표현식

SOOYEON·2022년 6월 5일
0

정규표현식

목록 보기
1/4

Link


Lesson 1: An Introduction, and the ABCs

TaskText
Matchabcdefg
Matchabcde
Matchabc
  • answer : abc
re.match('abc', text)


Lesson 1½: The 123s

TaskText
Matchabc123xyz
Matchdefine "123"
Matchvar g = 123;
  • answer : 123
re.match('.*123', text) # '.+123'
re.match('.*\d', text) # '.+\d'


Lesson 2: The Dot

TaskText
Matchabc123xyz
Matchdefine "123"
Matchvar g = 123;
  • answer : \.
re.match('.+\.', text)


Lesson 3: Matching specific characters

TaskText
Matchcan
Matchman
Matchfan
Skipdan
Skipran
Skippan
  • answer : [cmf]an
re.match('[cmf]an', text)


Lesson 4: Excluding specific characters

TaskText
Matchhog
Matchdog
Skipbog
  • answer : [^b]og
re.match('[^b]og', text)


Lesson 5: Character ranges

TaskText
MatchAna
MatchBob
MatchCpc
Skipaax
Skipbby
Skipccz
  • answer : [A-Z]
re.match('[A-Z]',text)


Lesson 6: Catching some zzz's

TaskText
Matchwazzzzzup
Matchwazzzup
Skipwazup
  • answer : z{2}
  • solution : waz{3,5}up
re.match('.+z{2}',text)


Lesson 7: Mr. Kleene, Mr. Kleene

TaskText
Matchaaaabcc
Matchaabbbbc
Matchaacc
Skipa
  • answer : a{2,4}.\D*c$
  • solution : aa+b*c+
    or a{2,4}b{0,4}c{1,2}
re.match('a{2,4}.\D*c$',text)


Lesson 8: Characters optional

TaskText
Match1 file found?
Match2 files found?
Match24 files found?
SkipNo files found.
  • answer : \d
    or \? or \d.*\?$
  • solution : \d+ files? found\?
re.match('\d.*\?$',text)


Lesson 9: All this whitespace

TaskText
Match1. abc
Match2. abc
Match3. abc
Skip4.abc
  • answer : \d.\s or \d*\s
  • solution : \d\.\s+abc
re.match('\d\.\s+abc',text)


Lesson 10: Starting and ending

TaskText
MatchMission: successful
SkipLast Mission: unsuccessful
SkipNext Mission: successful upon capture of target
  • answer : ^Mission.*\D
  • solution : ^Mission: successful$
re.match('^Mission.*\D',text)


Lesson 11: Match groups

TaskTextCapture Groups
Capturefile_record_transcript.pdffile_record_transcript
Capturefile_07241999.pdffile_07241999
Skiptestfile_fake.pdf.tmp
  • solution : ^(file.+)\.pdf$
re.match('^(file.+)\.pdf$',text)

Lesson 12: Nested groups

TaskTextCapture Groups
CaptureJan 1987(Jan 1987) (1987)
CaptureMay 1969(May 1969) (1969)
CaptureAug 2011(Aug 2011) (2011)
  • answer : ((\D+)(\d{4}))
  • solution : (\w+ (\d+))
re.match('((\D+)(\d{4}))',text)
re.match('(\w+ (\d+))',text)


Lesson 13: More group work

TaskTextCapture Groups
Capture1280x7201280 720
Capture1920x16001920 1600
Capture1024x7681024 768
  • answer : (\d{4})x(\d{3,4})
  • solution : (\d+)x(\d+)
re.match('(\d{4})x(\d{3,4})',text)
re.match('(\d+)x(\d+)',text)


Lesson 14: It's all conditional

TaskText
MatchI love cats
MatchI love dogs
SkipI love logs
SkipI love cogs
  • answer : I love +(cats|dogs)
re.match('I love +(cats|dogs)',text)


Lesson 15: Other special characters

Other special characters

0개의 댓글