[英]regex filtering repeatating items with numerics
我有以下項目列表
list1=['test_input_1','test_input_2','test_input_3','test_input_10','test_input_11']
我需要以下輸出-test_input_1
碼
for each in list1:
string1 = each
pattern = r'test_.*[1].*'
match = re.search(pattern,string1)
if match:
print 'matched=', match.group()
Output-
matched= test_input_1
matched= test_input_10
matched= test_input_11
Expected Output-
matched= test_input_1
另外,模式之前'r'和'u'之間有什么區別?
我不確定你的用例是什么,或者你想要做什么..你寫的代碼確實完成了應該做的事情....
看來你不能正確理解正則表達式......
我會打破test_.*[1].*
為你...
test_
:只是想在文本中找到“test_”。 .*
:這意味着任何字符( .
)任意次數( *
),這意味着它也可以是0。 [1]
:這意味着組中的任何字符,因此在這種情況下,給出的唯一字符是1
。 .*
:這意味着任何字符( .
)任意次數( *
),這意味着它也可以是0。 (再次) 所以你得到test_input_1
, test_input_10
和test_input_11
是test_input_1
,因為它們都遵循這種模式。
由於您只想捕獲與test_input_1
匹配的模式,因此使用正則表達式是沒有意義的......您只需將列表中的每個字符串與test_input_1
進行比較test_input_1
。
for item in list1:
if item == 'test_input_1':
# you found it!
print ("Found: test_input_1")
我不確定你要用這個來完成什么....
也許這樣的事情會幫助你更多:
for idx, item in enumerate(list1):
if item == 'test_input_1':
print ('Found "test_input_1" at index %s' % idx)
但是如果你需要在正則表達式中做同樣的想法,那么這樣的事情:
import re
def find_pattern(pattern, lst):
regex = re.compile(pattern)
for idx, item in enumerate(lst):
match = regex.match(item)
if not match:
continue
yield match.group(1), idx
list1=['test_input_1','test_input_2','test_input_3','test_input_10','test_input_11']
pat = r'(test_.*_1)\b'
for r in find_pattern(pat, list1):
print 'found %s at index %s' % r
>>>
found test_input_1 at index 0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.