这是我的字符串:
您的交易的一次性密码,在您的xxxx银行'借记/贷记/存款/ ....'的'xxxx'为inr'1897.00',结尾为'0000'的卡是0000“
xxxx
- 字符串,0000
- 数字
我想用单引号(')
获取所有值这就是我的尝试:
[a-z ]+, ([a-z]+)[a-z ]+([0-9\.]+)
直到这里才是正确的
现在我想获取(借记/贷记/ ...),我正在做:
在
上[a-z]+
银行[a-z]+[a-z ]+([0-9]+)[a-z ]+[0-9]
什么应该是更好的方式?
答案 0 :(得分:0)
您正在寻找的正则表达式只是r"'(.*?)'"
。下面是一个示例程序:
import re
regex = r"'(.*?)'"
test_str = "\"one time password for your transaction at, 'xxxx' of inr '1897.00' on your xxxx bank 'debit/credit/deposit/....' card ending '0000' is 0000\""
matches = re.finditer(regex, test_str)
for matchNum, match in enumerate(matches):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
哪个输出:
Match 0 was found at 44-50: 'xxxx'
Match 1 was found at 58-67: '1897.00'
Match 2 was found at 86-113: 'debit/credit/deposit/....'
Match 3 was found at 126-132: '0000'
在此处详细了解如何使用正则表达式:https://regex101.com/
答案 1 :(得分:0)
如果你想要单引号中的所有字符,
import re
string = "'xxxx' of inr '1897.00' on your xxxx bank 'debit/credit/deposit/....' card ending '0000' is 0000"
all_matches = re.findall(r"\'.+?\'",string)
print all_matches
答案 2 :(得分:0)
最安全,最高效的方法是匹配两个不是单引号的单引号之间的任何内容(在这种情况下,贪婪或懒惰无关紧要):
'[^']*'
代码示例:
import re
regex = r"'[^']*'"
test_str = '''one time password for your transaction at, 'xxxx' of inr '1897.00' \
on your xxxx bank 'debit/credit/deposit/....' card ending '0000' is 0000'''
matches = re.finditer(regex, test_str)
for match in matches:
print ("Match was found at {start}-{end}: {match}".format(start = match.start(), end = match.end(), match = match.group()))