473,386 Members | 1,969 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,386 software developers and data experts.

How do I find possible matches using regular expression?

Hi there,

I'm trying to do some predicting work over user input, here's my
question:

for pattern r'match me', the string 'no' will definitely fail to match,
but 'ma' still has a chance if user keep on inputting characters after
'ma', so how do I mark 'ma' as a possible match string?

Thanks a lot,

Andy

Nov 23 '06 #1
9 1381
"Andy" <ic*********@gmail.comwrote in message
news:11**********************@l39g2000cwd.googlegr oups.com...
Hi there,

I'm trying to do some predicting work over user input, here's my
question:

for pattern r'match me', the string 'no' will definitely fail to match,
but 'ma' still has a chance if user keep on inputting characters after
'ma', so how do I mark 'ma' as a possible match string?

Thanks a lot,

Andy
Maybe .startsWith might be more useful than re.match, since you are
predicting user input based on characters that have been typed so far.

-- Paul
Nov 23 '06 #2
The problem is the input will be much more complex than the example, it
could be something like "30 minutes later" where any string starting
with a number is a possible match.
Paul McGuire ¼g¹D¡G
"Andy" <ic*********@gmail.comwrote in message
news:11**********************@l39g2000cwd.googlegr oups.com...
Hi there,

I'm trying to do some predicting work over user input, here's my
question:

for pattern r'match me', the string 'no' will definitely fail to match,
but 'ma' still has a chance if user keep on inputting characters after
'ma', so how do I mark 'ma' as a possible match string?

Thanks a lot,

Andy
Maybe .startsWith might be more useful than re.match, since you are
predicting user input based on characters that have been typed so far.

-- Paul
Nov 23 '06 #3
Andy wrote:
The problem is the input will be much more complex than the example, it
could be something like "30 minutes later" where any string starting
with a number is a possible match.
so if I type "1", are you going to suggest all possible numbers
that start with that digit? doesn't strike me as very practical.

maybe you could post a more detailed example, where you clearly
explain what a pattern is and how it is defined, what prediction
means, and what you want to happen as new input arrives, so we
don't have to guess?

</F>

Nov 23 '06 #4

Andy wrote:
Hi there,

I'm trying to do some predicting work over user input, here's my
question:

for pattern r'match me', the string 'no' will definitely fail to match,
but 'ma' still has a chance if user keep on inputting characters after
'ma', so how do I mark 'ma' as a possible match string?
The answer is: Using regular expressions doesn't seem like a good idea.
If you want to match against only one target, then
target.startswith(user_input) is, as already mentioned, just fine.
However if you have multiple targets, like a list of computer-language
keywords, or the similar problem of an IME for a language like Chinese,
then you can set up a prefix-tree dictionary so that you can search the
multiple target keywords in parallel. All you need to do is keep a
"finger" pointed at the node you have reached along the path; after
each input character, either the finger gets pointed at the next
relevant node (if the input character is valid) or you return/raise a
failure indication.

HTH,
John

Nov 23 '06 #5
Andy wrote:
I'm trying to do some predicting work over user input, here's my
question:

for pattern r'match me', the string 'no' will definitely fail to match,
but 'ma' still has a chance if user keep on inputting characters after
'ma', so how do I mark 'ma' as a possible match string?
The following may or may not work in the real world:

import re

def parts(regex, flags=0):
candidates = []
for stop in reversed(range(1, len(regex)+1)):
partial = regex[:stop]
try:
r = re.compile(partial + "$", flags)
except re.error:
pass
else:
candidates.append(r)
candidates.reverse()
return candidates

if __name__ == "__main__":
candidates = parts(r"[a-z]+\s*=\s*\d+", re.IGNORECASE)
def check(*args):
s = var.get()
for c in candidates:
m = c.match(s)
if m:
entry.configure(foreground="#008000")
break
else:
entry.configure(foreground="red")
import Tkinter as tk
root = tk.Tk()
var = tk.StringVar()
var.trace_variable("w", check)
entry = tk.Entry(textvariable=var)
entry.pack()
root.mainloop()

The example lets you write an assignment of a numerical value, e. g

meaning = 42

and colours the text in green or red for legal/illegal entries.

Peter
Nov 23 '06 #6
OK, here's what I want...

I'm doing a auto-tasking tool in which user can specify the execution
rule by inputting English instead of a complex GUI interface(Normally a
combination of many controls). This way is way better user interaction.

So the problem comes down to "understanding" user input and
"suggesting" possible inputs when user is writing a rule.

Rules will be like "30 minutes later", "Next Monday", "Every 3 hours",
"3pm"...Sure this is an infinite collection, but it doesn't have to be
perfect , it might make mistakes given inputs like "10 minutes after
Every Monday noon".

The "suggesting" feature is even harder, I'm still investigating
possibilities.

Tried NLTK_Lite, I'm sure it can understands well a good user input,
but it is not doing good with some bad inputs("2 hours later here"),
bad inputs makes the toolkit fails to parse it. And NLTK also does not
help on the suggesting part.

Now I'm thinking manipulating regular expressions. I think it's
possible to come up with a collection of REs to understand basic
execution rules. And the question is again how to do suggestions with
the RE collection.

Any thoughts on this subject?

I'm not a native English speaker so...please, be mistake tolerant with
my post here:-)


"Fredrik Lundh дµÀ£º
"
Andy wrote:
The problem is the input will be much more complex than the example, it
could be something like "30 minutes later" where any string starting
with a number is a possible match.

so if I type "1", are you going to suggest all possible numbers
that start with that digit? doesn't strike me as very practical.

maybe you could post a more detailed example, where you clearly
explain what a pattern is and how it is defined, what prediction
means, and what you want to happen as new input arrives, so we
don't have to guess?

</F>
Nov 23 '06 #7
The seems good to me, I'll try it out, thanks for the posting.
"Peter Otten дµÀ£º
"
Andy wrote:
I'm trying to do some predicting work over user input, here's my
question:

for pattern r'match me', the string 'no' will definitely fail to match,
but 'ma' still has a chance if user keep on inputting characters after
'ma', so how do I mark 'ma' as a possible match string?

The following may or may not work in the real world:

import re

def parts(regex, flags=0):
candidates = []
for stop in reversed(range(1, len(regex)+1)):
partial = regex[:stop]
try:
r = re.compile(partial + "$", flags)
except re.error:
pass
else:
candidates.append(r)
candidates.reverse()
return candidates

if __name__ == "__main__":
candidates = parts(r"[a-z]+\s*=\s*\d+", re.IGNORECASE)
def check(*args):
s = var.get()
for c in candidates:
m = c.match(s)
if m:
entry.configure(foreground="#008000")
break
else:
entry.configure(foreground="red")
import Tkinter as tk
root = tk.Tk()
var = tk.StringVar()
var.trace_variable("w", check)
entry = tk.Entry(textvariable=var)
entry.pack()
root.mainloop()

The example lets you write an assignment of a numerical value, e. g

meaning = 42

and colours the text in green or red for legal/illegal entries.

Peter
Nov 23 '06 #8
This works well as a checking strategy, but what I want is a suggesting
list...

Maybe what I want is not practical at all?

Thanks anyway Peter.

Andy Wu
Andy Œ‘µÀ£º
The seems good to me, I'll try it out, thanks for the posting.
"Peter Otten дµÀ£º
"
Andy wrote:
I'm trying to do some predicting work over user input, here's my
question:
>
for pattern r'match me', the string 'no' will definitely fail to match,
but 'ma' still has a chance if user keep on inputting characters after
'ma', so how do I mark 'ma' as a possible match string?
The following may or may not work in the real world:

import re

def parts(regex, flags=0):
candidates = []
for stop in reversed(range(1, len(regex)+1)):
partial = regex[:stop]
try:
r = re.compile(partial + "$", flags)
except re.error:
pass
else:
candidates.append(r)
candidates.reverse()
return candidates

if __name__ == "__main__":
candidates = parts(r"[a-z]+\s*=\s*\d+", re.IGNORECASE)
def check(*args):
s = var.get()
for c in candidates:
m = c.match(s)
if m:
entry.configure(foreground="#008000")
break
else:
entry.configure(foreground="red")
import Tkinter as tk
root = tk.Tk()
var = tk.StringVar()
var.trace_variable("w", check)
entry = tk.Entry(textvariable=var)
entry.pack()
root.mainloop()

The example lets you write an assignment of a numerical value, e. g

meaning = 42

and colours the text in green or red for legal/illegal entries.

Peter
Nov 24 '06 #9
Andy wrote:
This works well as a checking strategy, but what I want is a suggesting
list...
Indeed, I was grossly misreading your question.

Peter
Nov 24 '06 #10

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

Similar topics

3
by: SROSeaner | last post by:
I am working on an ASP page that parses text using the VBScript.RegExp regular expression object. My reg expression right now is as follows: +\.+\.+/ And if find URL's no problem like: ...
9
by: Pete Davis | last post by:
I'm using regular expressions to extract some data and some links from some web pages. I download the page and then I want to get a list of certain links. For building regular expressions, I use...
5
by: Chris Lasher | last post by:
Hey guys and gals, This is a followup of my "Counting all permutations of a substring" thread (see...
32
by: Licheng Fang | last post by:
Basically, the problem is this: 'do' Python's NFA regexp engine trys only the first option, and happily rests on that. There's another example: 'oneself' The Python regular expression...
1
by: Allan Ebdrup | last post by:
I have a dynamic list of regular expressions, the expressions don't change very often but they can change. And I have a single string that I want to match the regular expressions against and find...
6
by: Peter Duniho | last post by:
So, I'm trying to learn how the Regex class works, and I've been trying to use it to do what I think ought to be simple things. Except I can't figure out how to do everything I want. :( If I...
1
by: RaZe | last post by:
Hi, I am absolutely a beginner in Python. How can I find an index of a regular expression pattern if it matches within a string? For example a string: 'abc:def' and I want to get the index of ':'...
6
by: tshad | last post by:
I have a filename that I want to extract the 1st set of numbers up to either a ".", "-", "_" or "~" and make that an int. Or I guess easier just take all the values that are 0-9 up to the 1st non...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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.