Reputation: 2698
i want to print out all substring in a string that has the following pattern +2 characters: for example get the substrings
$iwantthis*12
$and this*11
from the string;
$iwantthis*1231 $and this*1121
in the monent i use
print re.search('(.*)$(.*) *',string)
and i get $iwantthis*1231
but how can i limit the number of characters after the last pattern symbol * ?
Greetings
Upvotes: 2
Views: 69
Reputation: 239653
import re
data = "$iwantthis*1231 $and this*1121"
print re.findall(r'(\$.*?\d{2})', data)
Output
['$iwantthis*12', '$and this*11']
Upvotes: 2
Reputation: 500883
In [13]: s = '$iwantthis*1231 $and this*1121'
In [14]: re.findall(r'[$].*?[*].{2}', s)
Out[14]: ['$iwantthis*12', '$and this*11']
Here,
[$]
matches $
;.*?[*]
matches the shortest sequence of characters followed by *
;.{2}
matches any two characters.Upvotes: 2