473,385 Members | 1,676 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.

passing multiple strings to string.find()

How do I say:

x = string.find(files, 'this', 'that', 'the-other')

currently I have to write it like this to make it work:

x = string.find(files, 'this')
y = string.find(files, 'that')
z = string.find(files, 'the-other')

Jul 18 '05 #1
3 33821

"hokiegal99" <ho********@vt.edu> wrote in message
news:3F**************@vt.edu...
How do I say:

x = string.find(files, 'this', 'that', 'the-other')

currently I have to write it like this to make it work:

x = string.find(files, 'this')
y = string.find(files, 'that')
z = string.find(files, 'the-other')


Try this:

x, y, z = map(files.find, ['this', 'that', 'the-other'])

or, if you're just trying to find the first match:

re.search('this|that|the-other', files).start()
OTOH, you've hinted at an application that may not
appropriate for multiple string searches. Instead, look
at building a dictionary or list of files -- they are most
easily searched and better suited for associating other
data such as file sizes, etc.


Raymond Hettinger
Jul 18 '05 #2
On Thu, 07 Aug 2003 23:35:55 -0400, hokiegal99 <ho********@vt.edu> wrote:
How do I say:

x = string.find(files, 'this', 'that', 'the-other')

currently I have to write it like this to make it work:

x = string.find(files, 'this')
y = string.find(files, 'that')
z = string.find(files, 'the-other')


You might try the re module, e.g.,
import re
rxo = re.compile(r'this|that|the-other')
pos = 0
while 1:

... m = rxo.search(' Find this or the-other or that and this.', pos)
... if not m: break
... print '%4s: %s' % (m.start(), m.group())
... pos = m.end()
...
6: this
14: the-other
27: that
36: this

If some search strings have a common prefix, you'll have to put
the longest first in the regex, since re grabs the first match it sees.

Regards,
Bengt Richter
Jul 18 '05 #3
[Fredrik Lundh]
Francois Pinard wrote:
Given the above,

build_regexp(['this', 'that', 'the-other'])

yields the string 'th(?:is|at|e\\-other)', which one may choose to
`re.compile' before use.

the SRE compiler looks for common prefixes, so "th(?:is|at|e\\-other)" is
no different from "this|that|the-other" on the engine level.


Thanks for the note. So the `build_regexp' function is not useful after
all. It was indirectly written around a speed problem in the GNU regexp
engine, but seemingly, the Python regexp engine knows better already. AsI
wrote earlier, I first saw Emacs Lisp `regexp-opt' used within `enscript'..

A speed comparison between both methods shows that they are fairly
equivalent. A small difference is that `build_regexp', given that one of
the word is a prefix of another, automatically recognises the longest one,
while a naive regexp of '|'.join(words) recognises whatever happens to be
listed first. Of course, this is easily solved by sorting, then reversing
the word list before producing the naive regexp.

--
François Pinard http://www.iro.umontreal.ca/~pinard

Jul 18 '05 #4

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

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
5
by: Jack | last post by:
Hi, I need to pass multple variables in a link in order to go to a asp page with the two varables. The following are the values of the variables using response.write: <%'Response.Write Mypage...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
3
by: Mark | last post by:
Hi From what I understand, you can pass arrays from classic ASP to .NET using interop, but you have to change the type of the.NET parameter to object. This seems to be because classic ASP passes...
5
by: Jason | last post by:
Is there a mechanism in VB.NET that allows something like: If myVar In ("A","B","C") Then... The way I'm doing it now is: Select Case myVar Case "A","B","C" Or like this:
19
by: santosh | last post by:
Hi all, In the following program I allocate a block of pointers to type char, initialised to zero. I then point each of those pointers to a block of allocated memory of fixed size (33 bytes). A...
1
by: SteveDouglas | last post by:
Hi all, I am currently writing an application in VB.NET that has a lot of controls (treeviews/listviews/labels and so forth) that represent "things" that need to be draggable from place to place,...
2
by: rengask | last post by:
I got the code to find and replace within an open text file. ------------------ Private Sub cmdFile_Click() Dim strTemp As String txtFile = "" dlg.FileName = "*.*" dlg.ShowOpen ...
6
by: Andy Baker | last post by:
I am attempting to write a .NET wrapper for a C++ DLL file, but am having problems with passing strings as parameters. How should I be writing my C# function call when the C header file is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.