简体   繁体   English

Python正则表达式在某个点之前删除所有内容

[英]Python regex remove all before a certain point

Let's say I got a string:假设我有一个字符串:

F:\\Somefolder [2011 - 2012]\somefile

And I want to use regex to remove everything before: somefile我想使用正则表达式删除之前的所有内容: somefile

So the string I get is:所以我得到的字符串是:

somefile

I tried to look at the regular expression sheet, but i cant seem to get it.我试图查看正则表达式表,但我似乎无法理解。 Any help is great.任何帮助都很棒。

If you want the part to the right of some character, you don't need a regular expression:如果您想要某个字符右侧的部分,则不需要正则表达式:

f = r"F:\Somefolder [2011 - 2012]\somefile" 
print f.rsplit("\\", 1)[-1]
#  somefile

不知道你为什么要在这里使用正则表达式...

your_string.rpartition('\\')[-1]

How about this little guy?这个小家伙呢?

[^\\]+$

... in action ... ……在行动……

myStr = "F:\Somefolder [2011 - 2012]\somefile"
result = re.match( r'[^\\]+$', myStr, re.M|re.I)
if result:
   print result.group(0)
else:
   print "No match!!"

Adapted from:http://www.tutorialspoint.com/python/python_reg_expressions.htm改编自:http ://www.tutorialspoint.com/python/python_reg_expressions.htm

Python RegEx Tester: http://www.regexplanet.com/advanced/python/index.html Python RegEx 测试器: http : //www.regexplanet.com/advanced/python/index.html

I've got it like you want remove part of string before some point-word ("somefile" in your example)我已经得到它就像你想在一些点词之前删除部分字符串(在你的例子中是“somefile”)

>> a = 'F:\\Somefolder [2011 - 2012]\somefile'
>> point = 'somefile'
>> print a[a.index(point):]
somefile
from typing import Iterable

def remove_all_before(items: list, border: int) -> Iterable:
    return items[items.index(border):] if border in items else items

print(remove_all_before((1,2,3,4,5,6), 3))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM