Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 需要找到'$字';字符串模式_Python_Python 3.x_Regex - Fatal编程技术网

Python 需要找到'$字';字符串模式

Python 需要找到'$字';字符串模式,python,python-3.x,regex,Python,Python 3.x,Regex,我有一个大的文本文件,我必须找到所有以“$”开头,以“;”结尾的单词像$word import re text = "$h;BREWERY$h_end;You've built yourself a brewery." x = re.findall("$..;", text) print(x) 我希望我的输出像['$h;','$h_end;']我如何才能做到这一点?您可以使用 \$\w+; 看。详情: \$-a$字符 \w+-1+字母、数字、\u(=word)字符 -分号 : 我必须找

我有一个大的文本文件,我必须找到所有以“$”开头,以“;”结尾的单词像
$word

import re

text = "$h;BREWERY$h_end;You've built yourself a brewery."
x = re.findall("$..;", text)
print(x)
我希望我的输出像
['$h;','$h_end;']
我如何才能做到这一点?

您可以使用

\$\w+;
看。详情:

  • \$
    -a
    $
    字符
  • \w+
    -1+字母、数字、
    \u
    (=word)字符
  • -分号
:

我必须找出所有单词都以“$”开头,以“;”结尾比如$word

我会:

import re
text = "$h;BREWERY$h_end;You've built yourself a brewery."
result = re.findall('\$[^;]+;',text)
print(result)
输出:

['$h;', '$h_end;']
请注意,
$
需要转义(
\$
),因为它是其中之一。然后我匹配1次或多次出现的任何事件,但
和最后的
x=re.findall(\$[a-zA-Z+;”,text)
试试这个
“\$.*?”
['$h;', '$h_end;']