unexpected end of regular expression

落爺英雄遲暮 提交于 2019-12-02 08:56:42

问题


I want to get only the file name with extension from the path:

C:\\Users\\anandada\\workspace\\MyTestProject\\src\\OpenTest.c

The statement below,

fileName = re.match("[^\\]*.c$", fileName)

gives error:

unexpected end of regular expression

I am using python 3.3.2


回答1:


You need to double the doubled escapes again, or use a raw string instead:

fileName = re.match("[^\\\\]*.c$",fileName)

or

fileName = re.match(r"[^\\]*.c$",fileName)

otherwise first Python, then the regular expression compiler will interpret those backslashes, resulting in the ] being escaped:

>>> print("[^\\]*.c$")
'[^\]*.c$'

Also see the Blackslash Plague section of the Python Regex HOWTO.

Next, you need to be aware that re.match anchors to the start of the string. You'll probably want to use re.search() instead here. See the match() vs. search() section:

The match() function only checks if the RE matches at the beginning of the string while search() will scan forward through the string for a match. It’s important to keep this distinction in mind.

You may also want to escape the . in the .c part; . matches any character, so foobaric would also match; the i would satisfy the . pattern.

The re.match() and re.search() functions return a match object, not the matched part of the string. You'll have to extract that part explicitly:

fileName = re.search(r'[^\\]*\.c$', fileName).group()

Demo:

>>> import re
>>> fileName = 'C:\\Users\\anandada\\workspace\\MyTestProject\\src\\OpenTest.c'
>>> re.search(r'[^\\]*\.c$', fileName).group()
'OpenTest.c'



回答2:


It is because you don't use a raw string. The double backslash is interpreted as an escape for the closing square bracket. You need to write:

fileName = re.match(r"[^\\]*.c$", fileName)

with the raw string format \\ is seen as a literal backslash as expected.



来源:https://stackoverflow.com/questions/26403986/unexpected-end-of-regular-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!