Matching a string up to a word

Lothar lothar at kki.net.pl
Wed Sep 4 14:52:37 CEST 2002


Best library for operating on text is re (Regular Expressions)

import re

log = open("filename.log")
for line in log:
    re.match(r"^\d+ (.*)(\s\w+){4}$",line).group(1)




> I have a string that contains an ID number a file path and a date, kind
> of like this:
>
> 23545 /this/is a/path/to/a file Sun Sep 7 2002.
>
> I want to extract the file name.  If I could be sure that the file path
> had no spaces or if it would be quited it would be easy.  Unfortunatly
> many files will have spaces and changing the format of the output I'm
> dealing with isn't possible.  I do however know that the file name starts
> after the first whitspace and that it's always followed by a three letter
> day name (like Mon, Tue etc.)  Given this is there an easy way to extract
> the whole file path from the string.
>
> Dag
>





More information about the Python-list mailing list