Using wildcards...

George Yoshida ml at dynkin.com
Mon May 2 08:13:19 EDT 2005


Harlin Seritt wrote:
> {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 should look into re module.
regex has more flexible features for text processing than string
  module or methods.

- Regular expression operations
   http://docs.python.org/lib/module-re.html
- HOWTO
   http://www.amk.ca/python/howto/regex/

In your case, the code would go like this:

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

--
george

http://www.dynkin.com/



More information about the Python-list mailing list