473,385 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Using wildcards...

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

Jul 19 '05 #1
4 18168
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
Jul 19 '05 #2
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
Jul 19 '05 #3
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/
Jul 19 '05 #4
George that is what I'm looking for. Thanks, Harlin

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Bharath Dhurjati | last post by:
Hello, I am looking for documentation that specifies the following behavior exhibited by java. The following (assuming MyClass.class is accessible and has a main()) java MyClass * yields...
14
by: Alec S. | last post by:
Hi, I'm using JavaScript and Cookies for some customization in a web page. There may be several values in the cookie with names that are not known at runtime. I need a way of deleting them. ...
4
by: Evil Bert | last post by:
Is there anyway to compare a field with multiple string values using LIKE? Here's the statement I have now: SELECT * FROM list WHERE email LIKE CONVERT( _utf8 'hotmail@hotmail.com' USING latin1...
11
by: Shyguy | last post by:
I need to import a text file pretty much daily. I download the file and change the name to a standard name and then run the code to import the file into a table in my database. The problem is...
10
by: Alvaro Puente | last post by:
Hi all! Do any of you know if wildcards are accepted when calling rename() function? Thanks/Alvaro
2
by: Dennis | last post by:
I am trying to implement a "Find and Replace" dialog that allows using wildcards in the find string, much like the Find and Replace Dialogs in Ms Word, etc. Are there any references or examples on...
1
by: Anandan | last post by:
Hi, This is regarding Dataset Filter: WILDCARD CHARACTERS Both the * and % can be used interchangeably for wildcards in a LIKE comparison. If the string in a LIKE clause contains a * or %,...
0
by: Web learner | last post by:
Individual file download works fine. But when I try to download multiple files, for example using wildcards in the following code, I get error. How to solve it? System.Net.WebClient Client = new...
3
by: =?Utf-8?B?RGF2aWRN?= | last post by:
Hi, does anyone know if I can scan a subdirectory using wildcards? I do not see a way to do this using: string folder = @"C:\INPUT\CUSTOMERS\ID???BC.*" DirectoryInfo di = new...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.