Using wildcards...

H

Harlin Seritt

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?

Thanks,

Harlin Seritt
 
R

Roel Schroeven

Harlin said:
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:
['', ' 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}'.
 
G

George Yoshida

Harlin said:
{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:
['', ' Random text here. ', ' More text here. and so on.']
 
K

Kent Johnson

Harlin said:
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:['', ' Random text here. ', ' More text here. and so on.']

Kent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top