Issue
I want to find strings that have no 。
char in them, with an optional occurrence of this character at the end of the string.
I search some tips, like that, but didn't solve my problem.
^(?!\.)(?!.*\.$)(?!.*\.\.)[a-zA-Z0-9_.]+$
(?!\.) - don't allow . at start
(?!.*\.\.) - don't allow 2 consecutive dots
(?!.*\.$) - don't allow . at end
I tried to use
str_l = ["aaa。bbb。","aaa。","aaa"]
for str1 in str_l:
res1 = re.search(r'(.*?!。*$)', str1) #if 。not in string, return True
res2 = re.search(r'(?<!(。)。$)',str1) # if 。 only appear at the end of string, return True, but not solved
print(res1,res2)
I want to combine res1
and res2
to one regex, and the string results like False, True, True
.
Solution
You can use
import re
str_l = ["aaa。bbb。","aaa。","aaa"]
for str1 in str_l:
print(str1, '=>', bool(re.search(r'^[^。]*。?$', str1)))
Output:
# => aaa。bbb。 => False
aaa。 => True
aaa => True
See the Python demo. Details:
^
- start of string[^。]*
- zero or more chars other than the dot。?
- an optional dot$
- at the end of string.
To obtain the valid strings from the list using this regex, you can use
rx = re.compile(r'^[^。]*。?$')
print( list(filter(rx.search, str_l)) )
# => ['aaa。', 'aaa']
Answered By - Wiktor Stribiżew
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.