[英]Regex for digits in end of string Python
I want to get boolean for all strings having digits at the end of string. 我想为所有在字符串末尾具有数字的字符串获取布尔值。 For example
例如
import re
# list of strings
li = ['/Usein-kysytyt-kysymykset;jsessionid=0727CD5A45A05D3CBD5A26D459C34D9D.xxlapp11',
'/vaatteet/naisten-vaatteet/naisten-takit/c/120204',
'/pyoraily/pyorailyvarusteet/pyorankuljetuslaukut-ja-vannepussit/c/100818_8']
for i in li:
if(bool(re.match('\d+$', i))):
print(i)
So this should work and return me True
for li[1]
and li[2]
and False
for li[0]
but it is returning false for all elements in the list. 所以这应该工作,并返回我
True
为li[1]
和li[2]
和False
的li[0]
但它返回列表中的所有元素假的。 What is wrong here ? 这是怎么了
You can use re.findall()
您可以使用
re.findall()
for i in li:
if(bool(re.findall('\d+$', i))):
print(i)
Try this: 尝试这个:
for i in li:
#get last occurrence of that string
l = i[len(i) - 1]
#if it is a number then do following
if l.isdigit():
print(i)
The python docs about re.match
: 有关
re.match
的python文档:
If zero or more characters at the beginning of string match the regular expression pattern, return a corresponding MatchObject instance.
如果字符串开头的零个或多个字符与正则表达式模式匹配,则返回相应的MatchObject实例。
To find out if the last element of a string is a digit, use this instead: 要确定字符串的最后一个元素是否为数字,请改用以下命令:
for i in li:
if(bool(re.search(r'\d+$', i))):
print(i)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.