Using wildcards...

Kent Johnson kent37 at tds.net
Mon May 2 08:17:14 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

You can do this with the re module. For example

  >>> import re
  >>> s = '{1:1} Random text here. {1:2} More text here. and so on.'
  >>> re.split(r'\{[^}]+\}', s)
['', ' Random text here. ', ' More text here. and so on.']

If you want to be a little stricter in what you accept for the split you could look explicitly for 
digits:
  >>> re.split(r'\{\d+:\d+\}', s)
['', ' Random text here. ', ' More text here. and so on.']

Kent



More information about the Python-list mailing list