472,794 Members | 4,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Pattern matching with string and list

Hi,

I'd need to perform simple pattern matching within a string using a
list of possible patterns. For example, I want to know if the substring
starting at position n matches any of the string I have a list, as
below:

sentence = "the color is $red"
patterns = ["blue","red","yellow"]
pos = sentence.find($)
# here I need to find whether what's after 'pos' matches any of the
strings of my 'patterns' list
bmatch = ismatching( sentence[pos:], patterns)

Is an equivalent of this ismatching() function existing in some Python
lib?

Thanks,

Olivier.

Dec 12 '05 #1
5 5646
ol****@gmail.com wrote:
Hi,

I'd need to perform simple pattern matching within a string using a
list of possible patterns. For example, I want to know if the substring
starting at position n matches any of the string I have a list, as
below:

sentence = "the color is $red"
patterns = ["blue","red","yellow"]
pos = sentence.find($)
# here I need to find whether what's after 'pos' matches any of the
strings of my 'patterns' list
bmatch = ismatching( sentence[pos:], patterns)

Is an equivalent of this ismatching() function existing in some Python
lib?

Thanks,

Olivier.

As I think you define it, ismatching can be written as:
def ismatching(sentence, patterns): ... re_pattern = re.compile("(%s)\Z" % "|".join(patterns))
... return bool(re_pattern.match(sentence))
... ismatching(sentence[pos+1:], patterns) True ismatching(sentence[pos+1:], ["green", "blue"]) False (For help with regular expressions, see: http://www.amk.ca/python/howto/regex/)
or, you can ask the regexp engine to starting looking at a point you specify:
def ismatching(sentence, patterns, startingpos = 0): ... re_pattern = re.compile("(%s)\Z" % "|".join(patterns))
... return bool(re_pattern.match(sentence, startingpos))
... ismatching(sentence, patterns, pos+1) True

but, you may be able to save the separate step of determining pos, by including
it in the regexp, e.g.,
def matching(patterns, sentence): ... re_pattern = re.compile("\$(%s)" % "|".join(patterns))
... return bool(re_pattern.search(sentence))
... matching(patterns, sentence) True matching(["green", "blue"], sentence) False
then, it might be more general useful to return the match, rather than the
boolean value - you can still use it in truth testing, since a no-match will
evaluate to False
def matching(patterns, sentence): ... re_pattern = re.compile("\$(%s)" % "|".join(patterns))
... return re_pattern.search(sentence)
... if matching(patterns, sentence): print "Match" ...
Match

Finally, if you are going to be doing a lot of these it would be faster to take
the pattern compilation out of the function, and simply use the pre-compiled
regexp, or as below, its bound method: search:
matching = re.compile("\$(%s)\Z" % "|".join(patterns)).search
matching(sentence) <_sre.SRE_Match object at 0x01847E60> bool(_) True bool(matching("the color is $red but there is more")) False bool(matching("the color is $pink")) False bool(matching("the $color is $red")) True


HTH

Michael


Dec 13 '05 #2
On Mon, 12 Dec 2005 ol****@gmail.com wrote:
I'd need to perform simple pattern matching within a string using a list
of possible patterns. For example, I want to know if the substring
starting at position n matches any of the string I have a list, as
below:

sentence = "the color is $red"
patterns = ["blue","red","yellow"]
pos = sentence.find($)
I assume that's a typo for "sentence.find('$')", rather than some new
syntax i've not learned yet!
# here I need to find whether what's after 'pos' matches any of the
strings of my 'patterns' list
bmatch = ismatching( sentence[pos:], patterns)

Is an equivalent of this ismatching() function existing in some Python
lib?


I don't think so, but it's not hard to write:

def ismatching(target, patterns):
for pattern in patterns:
if target.startswith(pattern):
return True
return False

You don't say what bmatch should be at the end of this, so i'm going with
a boolean; it would be straightforward to return the pattern which
matched, or the index of the pattern which matched in the pattern list, if
that's what you want.

The tough guy way to do this would be with regular expressions (in the re
module); you could do the find-the-$ and the match-a-pattern bit in one
go:

import re
patternsRe = re.compile(r"\$(blue)|(red)|(yellow)")
bmatch = patternsRe.search(sentence)

At the end, bmatch is None if it didn't match, or an instance of re.Match
(from which you can get details of the match) if it did.

If i was doing this myself, i'd be a bit cleaner and use non-capturing
groups:

patternsRe = re.compile(r"\$(?:blue)|(?:red)|(?:yellow)")

And if i did want to capture the colour string, i'd do it like this:

patternsRe = re.compile(r"\$((?:blue)|(?:red)|(?:yellow))")

If this all looks like utter gibberish, DON'T PANIC! Regular expressions
are quite scary to begin with (and certainly not very regular-looking!),
but they're actually quite simple, and often a very powerful tool for text
processing (don't get carried way, though; regular expressions are a bit
like absinthe, in that a little helps your creativity, but overindulgence
makes you use perl).

In fact, we can tame the regular expressions quite neatly by writing a
function which generates them:

def regularly_express_patterns(patterns):
pattern_regexps = map(
lambda pattern: "(?:%s)" % re.escape(pattern),
patterns)
regexp = r"\$(" + "|".join(pattern_regexps) + ")"
return re.compile(regexp)

patternsRe = regularly_express_patterns(patterns)

tom

--
limited to concepts that are meta, generic, abstract and philosophical --
IEEE SUO WG
Dec 13 '05 #3
Taking you literally, I'm not sure you need regex. If you know or can
find position n, then can't you just:

sentence = "the color is $red"
patterns = ["blue","red","yellow"]
pos = sentence.find("$")
for x in patterns:
if x==sentence[pos+1:]:
print x, pos+1

But maybe I'm oversimplifying.

rpd

Dec 13 '05 #4
Even without the marker, can't you do:

sentence = "the fabric is red"
colors = ["red", "white", "blue"]

for color in colors:
if (sentence.find(color) > 0):
print color, sentence.find(color)

Dec 13 '05 #5
BartlebyScrivener wrote:
Even without the marker, can't you do:

sentence = "the fabric is red"
colors = ["red", "white", "blue"]

for color in colors:
if (sentence.find(color) > 0):
print color, sentence.find(color)

That depends on whether you're only looking for whole words:
colors = ['red', 'green', 'blue']
def findIt(sentence): .... for color in colors:
.... if sentence.find(color) > 0:
.... print color, sentence.find(color)
.... findIt("This is red") red 8 findIt("Fredrik Lundh") red 1


It's easy to see all the cases that this approach will fail for...
Dec 13 '05 #6

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

Similar topics

8
by: gsv2com | last post by:
One of my weaknesses has always been pattern matching. Something I definitely need to study up on and maybe you guys can give me a pointer here. I'm looking to remove all of this code and just...
176
by: Thomas Reichelt | last post by:
Moin, short question: is there any language combining the syntax, flexibility and great programming experience of Python with static typing? Is there a project to add static typing to Python? ...
3
by: Greg Lindstrom | last post by:
Hello- I'm running Python 2.2.3 on Windows XP "Professional" and am reading a file wit 1 very long line of text (the line consists of multiple records with no cr/lf). What I would like to do is...
9
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # Matching string patterns # # Sometimes you want to know if a string is of # particular pattern. Let's say in your website # you have converted all images...
1
by: Henry | last post by:
I have a table that stores a list of zip codes using a varchar column type, and I need to perform some string prefix pattern matching search. Let's say that I have the columns: 94000-1235 94001...
2
by: Joecx | last post by:
Hi If I want to copy files using a pattern like: I want all files on a directory that start with 20050822 to be copied to a different directory. I can't get file.copy or copyfile to accept *.*...
1
by: Sea Sharper | last post by:
Hi, C#, from a FileSystemWatcher I would like to catch all files with a *.* filter but then inside the event handler compare against a list of wildcards (eg '*.abc;*.def') Is there anywhere...
9
by: Jim Lewis | last post by:
Anyone have experience with string pattern matching? I need a fast way to match variables to strings. Example: string - variables ============ abcaaab - xyz abca - xy eeabcac - vxw x...
7
by: Captain Dondo | last post by:
I'm working on a terminal emulator for an embedded system. The key requirements are small size, code clarity, maintainability, and portability. We have machines that regularly see a service life...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.