简体   繁体   中英

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

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

Python RegEx Tester: 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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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