简体   繁体   English

在python中打印出$和* + 2个字符之间的所有字符串

[英]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 我想打印出具有以下模式+2个字符的字符串中的所有子字符串:例如获取子字符串

$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 * ? 我得到$iwantthis*1231但是如何限制最后一个图案符号*之后的字符数?

Greetings 问候

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. .{2}匹配任意两个字符。
import re
data = "$iwantthis*1231  $and this*1121"
print re.findall(r'(\$.*?\d{2})', data)

Output 产量

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

正则表达式可视化

Debuggex Demo Debuggex演示

RegEx101 Explanation RegEx101说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM