Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中实现正则表达式时,行继续后出现意外字符_Python_Regex_Python 2.7_Regex Group - Fatal编程技术网

在python中实现正则表达式时,行继续后出现意外字符

在python中实现正则表达式时,行继续后出现意外字符,python,regex,python-2.7,regex-group,Python,Regex,Python 2.7,Regex Group,我尝试使用正则表达式搜索文件夹中的文件,但当我尝试在变量中传递正则表达式时,如 file_to_search = re.search('Weekly' +(\d{2})[/.-](\d{2})[/.-](\d{4})$ + 'Scores.xlsx') 我试图搜索的文件模式是 2018年2月28日每周2次得分.xlsx 2018年5月3日每周记分.xlsx 到目前为止,我不在乎文件是否: 每周99.99.9999分数.xlsx 但是我得到了下面的错误,它指向这行的末尾 SyntaxError:

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

file_to_search = re.search('Weekly' +(\d{2})[/.-](\d{2})[/.-](\d{4})$ + 'Scores.xlsx')
我试图搜索的文件模式是

2018年2月28日每周2次得分.xlsx

2018年5月3日每周记分.xlsx

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

每周99.99.9999分数.xlsx

但是我得到了下面的错误,它指向这行的末尾

SyntaxError: unexpected character after line continuation character.
文件_to_search=re.search'Weekly'+\d{2}\d{2}\d{4}+'Scores.xlsx'

                                                          ^
重新搜索需要一个模式和一个文本。你漏掉了其中一个

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

你可能不是指.xlsx

你需要避开扩展点。您不需要转义日期中的点,因为它位于方括号内,是一个字符类

你需要考虑一下空间。文字空间在这里可以正常工作;如果可能的话,它可能是一个标签或一些更好的东西

我使用原始字符串文字r'…',这样我可以写\d而不是\\d等

现在大家一起:

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

让你的生活更简单:

>>> 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
>>> 

\d{2}[/.-]\d{2}[/.-]\d{4}$不是有效的表达式。正则表达式在Python中以字符串形式编写。您明确地将其从字符串中漏掉了。关于startswith和endswith的内容是什么?re.search需要一个模式和一个文本。。所以,这是你希望找到模式的字符串。编辑:您说我正在尝试使用regex搜索文件夹中的文件,结合您刚才的评论,我担心您可能不知道regexp的功能。他们在字符串中查找文本。他们在你的磁盘上找不到文件。要查找文件,可以使用os.listdir、os.walk、glob模块,我确信我遗漏了一些东西;一旦你有了一个潜在候选人的列表,你就可以使用正则表达式更精确地过滤掉他们。所以我实现这一点的方法是,定义一个变量,比如文件_to_search=re.searchr'^Weekly\d{2}[/.-]\d{2}[/.-]\d{4}Scores\.xlsx$'def search\u file:for file in os.listdirsource:if fnmatch.fnmatchfile,file\u to\u search:返回True返回False if search\u file:print'file exists'否则:print'file not found'注释对于多行代码来说是非常糟糕的,对于具有缩进语法的Python来说,这是双重的。fnmatch匹配和regexp匹配是两种不同类型的匹配-如果您正试图这样做,则无法为fnmatch提供正则表达式。即使如此,编译后的正则表达式是由pinnele=re.compileregex_string创建的,然后您可以将其用作pinnele.searchhaystack;re.searchregex_string,haystack是一种同时执行此操作的快捷方式。在您的示例中,对于os.listdir中的文件。。。是正确的;之后,您可以执行if re.searchregex_string,file:,也可以执行fnmatch.fnmatchfnmatch_string,file,在这种情况下不需要重新搜索。fnmatch的模式在顶部描述,与正则表达式没有任何关系,只是它们都用于文本匹配。我对其余的函数没有意见。我现在得到的错误是>,所以我只想知道在变量中声明正则表达式时需要传递的第二个参数是什么?