Aakib
Aakib

Reputation: 89

Unexpected Character after line continuation while implementing regex in python

I am trying to search a file in a folder using regex, but when i try to pass the regex in a variable like

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

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.

SyntaxError: unexpected character after line continuation character.

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

                                                          ^

Upvotes: 0

Views: 508

Answers (2)

lenik
lenik

Reputation: 23556

Make your life simpler:

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

I hope this answers your question =)

Of if you want to work with files:

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

Upvotes: 0

Amadan
Amadan

Reputation: 198436

  • re.search needs a pattern and a text. You left one of them out.

  • Python does not have a literal syntax for regular expressions, which means all regular expressions in Python need to be strings.

  • You probably didn't mean .xlsx.

  • You need to escape the extension dot. (You do not need to escape the dot in the date, since it is inside square brackets, a character class.)

  • You need to account for space. A literal space works okay here; if it's possible it might be a tab or something \s would be preferable.

  • I use raw string literal r'...' so I can write \d instead of \\d etc.

All together now:

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

Upvotes: 1

Related Questions