Python regex over multiple newlines

2019-08-09 08:13发布

I have a string which contains multiple file paths, some of which contain arbitrary newlines within the path, and I want to parse the string using python so that only the filenames and extensions remain.

For example:

a/b/c/d/file1.c  
a/b/c/d/e/f/g/h/1/2/3/4/5/foo.c  
dir1/dir2/newlinedir  
/nextlinedir/bar.c

should be parsed to give output:

file1.c
foo.c
bar.c

I am using the following regular expression (the groups for the filename and extension must be separate for later purposes):

path_regex = re.compile(r'.*\/([^\/\.]*)(\.c){0,1}$', re.MULTILINE)
path_regex.sub(r'\g<1>\g<2>', input_string)

This will work on strings with single line paths but not paths that contain newlines. What should I do?

5条回答
别忘想泡老子
2楼-- · 2019-08-09 08:38

This is technically not what you are asking for, but maybe regex here is not the right tool, since now you have two problems.

I think this is what you are searching for:

pydoc os.path.basename

So try with this:

map(os.path.basename, text.split('\n'))
查看更多
萌系小妹纸
3楼-- · 2019-08-09 08:40
^([\s\S]*?\/)(\w+\.c)

Try this.See demo.This will work multiline too.Use m or multiline flag.

https://regex101.com/r/rX1tE6/7

查看更多
三岁会撩人
4楼-- · 2019-08-09 08:52

Try this regex: (?:.*\/)(.+)\.(.+)

Use \1 to access filename and \2 to access extension

DEMO

查看更多
啃猪蹄的小仙女
5楼-- · 2019-08-09 08:59

This simple regex also works and you can access the filename with extension using \1

([^/]*\.\w+)
查看更多
The star\"
6楼-- · 2019-08-09 09:03

You may try this,

>>> s = '''a/b/c/d/file1.c  
a/b/c/d/e/f/g/h/1/2/3/4/5/foo.c  
dir1/dir2/newlinedir  
/nextlinedir/bar.c'''
>>> print(re.sub(r'(?s).*?([^/]+\.c)', r'\1\n', s))
file1.c
foo.c
bar.c

or

>>> print(re.sub(r'(?s).*?([^/]+)(\.[^.\n]+)(?=$|\n)', r'\1\2\n', s))
file1.c  
foo.c  
bar.c
查看更多
登录 后发表回答