501 - not implemented
501 - not implemented

Reputation: 2698

print out all strings between $ and *+2 characters in python

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

Answers (2)

thefourtheye
thefourtheye

Reputation: 239653

import re
data = "$iwantthis*1231  $and this*1121"
print re.findall(r'(\$.*?\d{2})', data)

Output

['$iwantthis*12', '$and this*11']

Regular expression visualization

Debuggex Demo

RegEx101 Explanation

Upvotes: 2

NPE
NPE

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

Related Questions