[英]searching string, exclude certain substrings
I am searching a string 000295-0000108352@ap.ipaccess.com
for 108352
, this should return true. 我正在搜索字符串
000295-0000108352@ap.ipaccess.com
获取108352
,这应该返回true。 However if I search for a substring of this it should not return true. 但是,如果我搜索它的子字符串,它不应该返回true。 Eg if I searched for
08352
, this is missing the 1 so that would be false. 例如,如果我搜索
08352
,则缺少1,因此这将是错误的。 How should I accomplish this? 我该怎么做到这一点?
I was searching like this: 我这样搜索:
for item in parse:
if element in item:
where element
is 08352
and parse is several strings in a list. 其中
element
是08352
,parse是列表中的几个字符串。 This is returning the positives I don't want. 这回归了我不想要的积极因素。
perhaps I should look for a pattern? 也许我应该寻找一种模式? After each string I search for is a
@
I notice, also before each one is a 0
. 在我搜索的每个字符串之后是
@
通知,也是在每个字符串为0
。 So perhaps a regex? 那么也许是一个正则表达式? And somehow incorporate it into my
for
and if
? 并以某种方式将它加入我的
for
和if
?
Edit: what if I prepend "00" to the search string and add @ at the end? 编辑:如果我在搜索字符串前加“00”并在结尾添加@会怎么样? Like:
喜欢:
if "00"+access_point_id+"@" in item:
A simple infix search should suffice: 简单的中缀搜索就足够了:
found = ("0%s@" % element) in item
a regular expression like -0+(\\d+)@
is safer, though: 像
-0+(\\d+)@
这样的正则表达式更安全:
m = re.search(r"-0+(\d+)@", item)
found = m and m.group(1) == element
If you are looking for a string of 10 digits, you can add padding to the string you search for: 如果要查找10位数的字符串,可以在搜索的字符串中添加填充:
>>> '{:0>10}'.format('08352')
'0000008352'
>>> '-{:0>10}@'.format('08352')
'-0000008352@'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.