Open EmbraceLife opened 5 years ago
互动可视化的为你解读 r'/([^/]+)_\d+.jpg
, 请前往regexr.com/48vci
pat = r'/([^/]+)_\d+.jpg$'
$
= 搜索从这里开始.jpg
= 一摸一样的字符\d
= 数字, \d+
= 任意多数字_
= 一摸一样的字符()
= 分组, ([])
= 一组字母, ([^/])
= 一组字母不含/, ([^/]+)
= 任意多字母不含//
, /([^/]+)_\d+.jpg$
= 搜索以 /
结束因此,我们可以顺利从“folder1/folder2/mycats/Abyssinian_1.jpg”中,解读出“Abyssinian_1.jpg”。但我们要的是“Abyssinian",怎么办?
()
分组能力,让我们用.group(1)
顺利从“Abyssinian_1.jpg”中提取出“Abyssinian"。
Notes from @PoonamV on forum
Regular expressions
we can use a regular expression by importing regular expression ‘re’ package in python, to do this. Regular expressions are a way to search a string in text using pattern matching methods.
Let’s deconstruct this regex pattern,
/([^/]+)_\d+.jpg$
by reading it backward:$
.jpg
\d
_
()
[]
^/
( [ ^/ ] + )
/
r
\d
would have to be written as\\d
so that Python doesn’t interpret it to be a special character.So, this regex pattern will give us a string
considering search string was
Further, by using the fact that the actual name of the breed is in the first parenthesized group of the regular expression, the actual breed name
can be obtained by using ‘.group(1)’ wherever the search on the regular expression is performed.See this for details.
credits @dreambeats
See this blog post to understand this regular expression in detail. The python documentation has a tutorial on regular expressions. RegexOne provides a simple interactive intro to regular expressions.