【发布时间】:2017-12-25 18:38:00
【问题描述】:
我对 Regex 很陌生,我在 javascript 中搜索了很长时间,我希望有人回复了从 python 转换的 javascript 中的 regex 的详细解释。
import re
regex = r"""
^(
(?P<ShowNameA>.*[^ (_.]) # Show name
[ (_.]+
( # Year with possible Season and Episode
(?P<ShowYearA>\d{4})
([ (_.]+S(?P<SeasonA>\d{1,2})E(?P<EpisodeA>\d{1,2}))?
| # Season and Episode only
(?<!\d{4}[ (_.])
S(?P<SeasonB>\d{1,2})E(?P<EpisodeB>\d{1,2})
| # Alternate format for episode
(?P<EpisodeC>\d{3})
)
|
# Show name with no other information
(?P<ShowNameB>.+)
)
"""
test_str = ("archer.2009.S04E13\n"
"space 1999 1975\n"
"Space: 1999 (1975)\n"
"Space.1999.1975.S01E01\n"
"space 1999.(1975)\n"
"The.4400.204.mkv\n"
"space 1999 (1975)\n"
"v.2009.S01E13.the.title.avi\n"
"Teen.wolf.S04E12.HDTV.x264\n"
"Se7en\n"
"Se7en.(1995).avi\n"
"How to train your dragon 2\n"
"10,000BC (2010)")
matches = re.finditer(regex, test_str, re.MULTILINE | re.VERBOSE)
for matchNum, match in enumerate(matches):
matchNum = matchNum + 1
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
【问题讨论】:
-
这可能对 Matching TV and Movie File names with Regex 有帮助,但它不是 js
-
您不能轻易将其转换为 JS 正则表达式,因为
(?<!\d{4}[ (_.])后面有一个否定的lookbehind。 -
@SudhirBastakoti 我就是从那里得到这个的。
-
@Wiktor 我只需要匹配标题和季号。
标签: javascript python regex