I have a string and a position in this string. I want to find out if the last non space character before this position is one of the characters in a given set. Can I do this using regex? I couldn't figure it out on my own.
Example with a set of characters (?, |, :):
foo('blah? test', pos=6) is True
foo('blah? test', pos=7) is False
With some help from Regex:
In [93]: def is_matched(text, pos, chars='?|!'):
...: text = text[:pos]
...: matched = re.search(r'.*(\S)(?=\s*$)', text)
...: return matched.group(1) in chars if matched else False
...:
In [94]: is_matched('blah? test', pos=6)
Out[94]: True
In [95]: is_matched('blah? test', pos=7)
Out[95]: False
.*(\S)(?=\s*$)
:
.*
matches any characters upto last non-space character
(\S)
matches the last non-space character and put it in a captured group
The zero-width postive lookahead (?=\s*$)
makes sure the pattern is followed by zero-more spaces only till end
Assuming that you want 0 indexed strings
def foo(text, pos):
return text[pos] in ['?','|','!']
foo('blah? test', pos=4) // True
foo('blah? test', pos=5) // False
You don't really need regex for this. You can very easily use any
and list comprehension .
s = 'blah? test'
print(any(v in s[4] for v in '?|!'))
Returns True
.
Changing s[4]
to s[5]
results in False
You don't need to use a regex:
def foo(s, pos, chars='?|!'):
for i in range(pos - 1, -1, -1):
if s[i] == ' ':
continue
return s[i] in chars
return False
print(foo('blah? test', pos=6))
If you had to use a regex:
def foo(s, pos, chars='?|!'):
l = re.findall(r'[^ ]', s[:pos]) # find all non-blank characters in first pos - 1 characters
if not l:
return False
return l[-1] in chars
You don't need regex here. Remove spaces at the end of a slice, if they are, and compare the last char
def is_matched(text, pos, chars='?|!'):
return text[:pos].rstrip()[-1] in chars
is_matched('blah? test', pos=6) #True
is_matched('blah? test', pos=7) #False
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.