python正则匹配换行符,Python正则表达式在多个换行符上

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?

解决方案^([\s\S]*?\/)(\w+\.c)

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值