Using wildcards...
Roel Schroeven
rschroev_nospam_ml at fastmail.fm
Mon May 2 07:54:35 EDT 2005
Harlin Seritt wrote:
> I looked all over the net but could not find if it is possible to
> insert wildcards into strings. What I am trying to do is this: I am
> trying to parse text from a Bible file. In case you're not familiar
> with the way the Bible organizes itself, it is broken down into Books >
> Chapters > Verses. The particular text I am working with are organized
> into Book files (*.txt -- flat text file). Here is what the file looks
> like:
>
> {1:1} Random text here. {1:2} More text here. and so on.
>
> Of course the {*} can be of any length, so I can't just do .split()
> based on the length of the bracket text. What I would like to do is to
> .split() using something akin to this:
>
> textdata.split('{*}') # The '*' being a wildcard
>
> Is this possible to do? If so, how is it done?
You can use the split function in the re module with a suitable regular
expression:
>>> re.split('{\d+:\d+}', textdata)
['', ' Random text here. ', ' More text here. and so on.']
{\d+:\d+} means 'match {, then one or more digits, then :, then one or
more digits, then }'.
re.split('{.*}', textdata) would be a more direct translation of your
wildcard, but that doesn't work: .* matches as much as possible, so in
your example it would match '{1:1} Random text here. {1:2}' instead of
just '{1:1}' and '{1:2}'.
--
If I have been able to see further, it was only because I stood
on the shoulders of giants. -- Isaac Newton
Roel Schroeven
More information about the Python-list
mailing list