I'd like to create regex to match if they're substring of commands.
Example: configure terminal
Match if have at least: conf t
I tried to use : r'conf(igure)?\\s*t(erminal)?'
but it matched something like "conf txxxxx" as well. also, it's not matched "config t"
my questions is I'd like to create that match something like these.
Match: config ter configu term conf t
not matched: confgure tminal
if match optional, it's need to be in order.
Thanks!
A regex is not a very good solution, as it is not particularly suitable for such a test, and neither is it easy configurable, maintainable, and expandable.
Better is to write a separate function that tests a single input i
against a possible match m
and returns True
if
len(i) >= minimum_length_required
, and i
matches that length from m
. This works for single-word entries:
def partialMatch(entry, full, minimum):
return len(entry) >= minimum and entry == full[:len(entry)]
>>> print (partialMatch('con', 'configure', 4))
False
>>> print (partialMatch('config', 'configure', 4))
True
>>> print (partialMatch('confiture', 'configure', 4))
False
but it takes more work with multi-word commands because each separate word must be checked – and, presumably, there is a long list of possible commands. However, the general idea should be something like this:
def validate(entry, cmd_list):
entry = entry.split()
if len(entry) != len(cmd_list):
return False
for index,word in enumerate(entry):
if not partialMatch(word, cmd_list[index].replace('#',''), cmd_list[index].find('#')):
return False
return True
where cmd_list
contains a list of allowed entries and a #
character matches the position of the minimum entry text. So you can do
>>> print (validate ('conf', ['conf#igure', 't#erminal']))
False
>>> print (validate ('conf t', ['conf#igure', 't#erminal']))
True
>>> print (validate ('configure t', ['conf#igure', 't#erminal']))
True
>> print (validate ('conf #', ['conf#igure', 't#erminal']))
False
(Of course you typically would not store the valid commands inside this call itself but in a longer list, and loop over it to find a valid command.)
Here is Example
s="conf fxxx "
if not s.find('conf t'):
print('yes')
else:
print('no')
只是在这里详述@ usr2564301注释,
import re pattern = r'conf(i(g(u(r(e)?)?)?)?)?\\st(e(r(m(i((n(a(l)?)?)?))?)?)?)?' text='config t' print(re.match(pattern, text))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.