将正则表达式python转换为javascript

将正则表达式python转换为javascript,javascript,python,regex,Javascript,Python,Regex,我对正则表达式非常陌生,我在javascript中搜索了很长一段时间,我希望有人用javascript详细解释正则表达式,并将其从python转换而来 import re regex = r""" ^( (?P<ShowNameA>.*[^ (_.]) # Show name [ (_.]+ ( # Year with possible Season and Episode (?P<ShowYearA&g

我对正则表达式非常陌生,我在javascript中搜索了很长一段时间,我希望有人用javascript详细解释正则表达式,并将其从python转换而来

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)))
重新导入
正则表达式=r“”
^(
(?P.[^(.])#显示名称
[ (_.]+
(#年份,可能有季节和情节
(?P\d{4})
([(.]+S(?P\d{1,2})E(?P\d{1,2}))?
|#仅限季和集
(?\d{1,2})E(?P\d{1,2})
|#插曲的替代格式
(?P\d{3})
)
|
#显示名称而不显示其他信息
(?P.+)
)
"""
test_str=(“archer.2009.S04E13\n”
“空间1999 1975\n”
“空间:1999(1975)\n”
“Space.1999.1975.S01E01\n”
“空间1999.(1975)\n”
“文件名为.4400.204.mkv\n”
“空间1999(1975)\n”
“v.2009.S01E13.the.title.avi\n”
“Teen.wolf.S04E12.HDTV.x264\n”
“Se7en\n”
“Se7en.(1995).avi\n”
“如何训练您的龙2\n”
“公元前10000年(2010年)”)
matches=re.finditer(regex、test_str、re.MULTILINE | re.VERBOSE)
对于matchNum,在枚举中匹配(匹配项):
matchNum=matchNum+1
打印(“在{start}-{end}:{Match}找到了Match{matchNum}”。格式(matchNum=matchNum,start=Match.start(),end=Match.end(),Match=Match.group())
对于范围(0,len(match.groups())中的groupNum:
groupNum=groupNum+1
打印(“在{start}-{end}:{Group}找到的组{groupNum}”。格式(groupNum=groupNum,start=match.start(groupNum),end=match.end(groupNum),Group=match.Group(groupNum)))

遗憾的是,没有简单的方法将Python正则表达式转换为Javascript正则表达式,因为Python正则表达式比Javascript正则表达式更健壮

Javascript缺少了一些功能性的东西,如负面外观滞后和递归,但它缺少了更多的语法工具,如详细语法和命名捕获组

常规捕获组=
()

命名捕获组=
(?P)

verbose正则表达式=
“find me”#此正则表达式忽略注释和空格”

非详细正则表达式=
“它按字面意思处理空白”

因此,如果我们将您的命名捕获组转换为常规(编号)捕获组
如果我们把冗长的语法转换成常规语法。 然后,该正则表达式将是有效的Javascript正则表达式,在Javascript中,它看起来像:
regex=
/^(.[^(.])[(.]+(\d{4})([(.]+S(\d{1,2})E(\d{1,2}))?|(

正如您所看到的,Javascript版本非常难看,因为它没有详细的语法或命名的捕获组

Javascript没有findall的直接等价物,因此您必须创建/找到一个等价物。


将来我还强烈建议去regexr.com学习regex,特别是javascript regex。

这可能会有帮助,但不是JS。你不能很容易地将它转换为JS regex,因为有一个反向查找
(?。@SudhirBastakoti,我就是从那里得到这个的。@Wiktor,我只需要标题和赛季号的匹配。
// group 2 = ShowNameA
// group 4 = ShowYearA
// group 6 = SeasonB
// group 7 = EpisodeC
// group 8 = ShowNameB