0

This is my string:

one time password for your transaction at, 'xxxx' of inr '1897.00' on your xxxx bank 'debit/credit/deposit/....' card ending '0000' is 0000"

xxxx - string, 0000 - numbers

I want to fetch all values in single quotes(')

this is what I have tried:

[a-z ]+, ([a-z]+)[a-z ]+([0-9\.]+) till here it is correct

now i want to fetch (debit/credit/...), I am doing:

on your [a-z]+ bank [a-z]+[a-z ]+([0-9]+)[a-z ]+[0-9]

What should be the better way?

Ivan Vinogradov
  • 3,071
  • 6
  • 22
  • 27

3 Answers3

0

The regex you are looking for is simply r"'(.*?)'". An example program below:

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()))

Which outputs:

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'

Learn more about using regex here: https://regex101.com/

Grant Williams
  • 1,379
  • 10
  • 22
0

If you want all the characters in the single quotes,

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
Kenstars
  • 611
  • 4
  • 10
0

The safest and most performant way to do this is to match anything between two single quotes that is not a single quote (greedy or lazy does not matter in this case):

'[^']*'

Demo

Code sample:

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()))
wp78de
  • 16,078
  • 6
  • 34
  • 56