【问题标题】:Unexpected Character after line continuation while implementing regex in python在python中实现正则表达式时行继续后出现意外字符
【发布时间】:2018-08-16 04:47:59
【问题描述】:

我正在尝试使用正则表达式搜索文件夹中的文件,但是当我尝试在变量中传递正则表达式时

file_to_search = re.search('Weekly' +(\d{2})[/.-](\d{2})[/.-](\d{4})$ + 'Scores.xlsx')

我要搜索的文件模式是

每周 02.28.2018 Scores.xlsx

每周 03.05.2018 Scores.xlsx

到目前为止,我不在乎文件是否为:

每周 99.99.9999 分数.xlsx

但我得到以下错误指向行尾。

SyntaxError: unexpected character after line continuation character.

file_to_search = re.search('Weekly' +\d{2}\d{2}\d{4}+ 'Scores.xlsx')

                                                          ^

【问题讨论】:

  • (\d{2})[/.-](\d{2})[/.-](\d{4})$ 不是有效的表达式。正则表达式在 Python 中被写成字符串。你特地把它排除在你的字符串之外。
  • 想知道startswith()endswith() 吗?

标签: python regex python-2.7 regex-group


【解决方案1】:
  • re.search 需要一个模式和一个文本。你漏掉了其中一个。

  • Python 没有正则表达式的文字语法,这意味着 Python 中的所有正则表达式都必须是字符串。

  • 你可能不是指.xlsx

  • 您需要转义扩展点。 (您不需要对日期中的点进行转义,因为它在方括号内,是一个字符类。)

  • 您需要考虑空间。文字空间在这里可以正常工作;如果可能的话,它可能是一个标签或\s 会更可取。

  • 我使用原始字符串文字 r'...',所以我可以写 \d 而不是 \\d 等。

现在大家一起:

match = re.search(r'^Weekly \d{2}[/.-]\d{2}[/.-]\d{4} Scores\.xslx$', file_to_test)

【讨论】:

  • re.search 需要一个模式和一个文本。”。因此,这是您希望找到模式的字符串。编辑:您说“我正在尝试使用正则表达式搜索文件夹中的文件”,再加上您刚才的评论,我担心您可能不知道正则表达式的作用。他们在字符串中查找文本。他们在您的磁盘上找不到文件。要查找文件,您可以使用os.listdiros.walkglob 模块,我确定我会遗漏一些内容;获得潜在候选人列表后,您可以使用正则表达式更精确地过滤掉它们。
  • 所以我实现这个的方式是,定义一个类似这样的变量 file_to_search = re.search(r'^Weekly \d{2}[/.-]\d{2}[/ .-]\d{4} 分数\.xlsx$') def search_file(): for file in os.listdir(source): if fnmatch.fnmatch(file, file_to_search): return True return False if search_file(): print ('文件存在') else: print('文件未找到')
  • 注释对于多行代码来说是非常糟糕的,对于具有缩进语法的 Python 来说,这是双倍的。 fnmatch 匹配和 regexp 匹配是两种不同类型的匹配 - 如果这是你想要做的,你不能给 fnmatch 一个正则表达式。即便如此,编译后的正则表达式是由needle = re.compile(regex_string)创建的,那么你可以将其用作needle.search(haystack)re.search(regex_string, haystack) 是一个快捷方式,可以一起执行此操作。
  • 在您的示例中,for file in os.listdir(...) 是正确的;之后,你可以使用if re.search(regex_string, file):你可以使用fnmatch.fnmatch(fnmatch_string, file)(在这种情况下你不需要re.search)。 fnmatch 的模式在 corresponding doc page 的顶部进行了描述,并且与正则表达式完全无关(除了它们都用于文本匹配这一事实之外)。跨度>
  • 其余功能我都很好。我现在得到的错误是 >>>,所以我只想知道在变量中声明正则表达式时需要传递的第二个参数是什么?
【解决方案2】:

让您的生活更简单:

>>> import re
>>> matcher = re.compile( r'Weekly \d\d\.\d\d\.\d\d\d\d Scores.xlsx' )
>>> a = '''The file pattern I am trying to search is
... 
... Weekly 02.28.2018 Scores.xlsx
... 
... Weekly 03.05.2018 Scores.xlsx
... 
... As of now I dont care if the file is:
... 
... Weekly 99.99.9999 Scores.xlsx
... 
... But I get the below error pointing at the end of the line.'''
>>> matcher.findall( a )
['Weekly 02.28.2018 Scores.xlsx', 'Weekly 03.05.2018 Scores.xlsx', 'Weekly 99.99.9999 Scores.xlsx']
>>>

我希望这能回答你的问题 =)

如果你想处理文件:

>>> filenames = matcher.findall( a )
>>> filenames.append( 'somefile.txt' )
>>> for f in filenames : print f, matcher.match( f ) is not None
... 
Weekly 02.28.2018 Scores.xlsx True
Weekly 03.05.2018 Scores.xlsx True
Weekly 99.99.9999 Scores.xlsx True
somefile.txt False
>>> 

【讨论】:

    猜你喜欢
    • 2016-08-08
    • 1970-01-01
    • 1970-01-01
    • 2019-09-26
    • 1970-01-01
    • 2019-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多