For example: I have a string “abcde2011-09-30.log”, I want to check if this string matchs “(\d){4}-(\d){2}-(\d){2}” ( dont think it has correct syntax, but you get the idea). And I need to split the string into 3 parts: (abcde),(e2011-09-30), (.log). How can I do it in python? Thanks.
Share
There’s a split method in the
remodule that should work for you.If you don’t actually want the date as part of the returned list, just omit the parentheses around the regular expression so that it doesn’t have a capturing group:
Be advised that if the pattern matches more than once, i.e. if there is more than one date in the filename, then this will split on both of them. For example,
If this is a problem, you can use the
maxsplitargument tosplitto only split it once, on the first occurrence of the date: