Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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_Regex - Fatal编程技术网

Python 带下划线字符串的正则表达式

Python 带下划线字符串的正则表达式,python,regex,Python,Regex,我想在使用正则表达式解析的文件中捕捉以下行 types == "EQUAL_NUM_SEQUENTIAL_LBAS": 为此,我使用以下代码 variable = 'types' for i in data: if re.search(re.escape(variable) + r"\s\=\=\s^[A-Z_]+$", i): print "yyy" break 此处的数据是解析文件中的行列表。我写的表达式有什么错误?如果要匹配仅由大写字母(可能由下

我想在使用正则表达式解析的文件中捕捉以下行

types == "EQUAL_NUM_SEQUENTIAL_LBAS":
为此,我使用以下代码

variable = 'types'
for i in data:
    if re.search(re.escape(variable) + r"\s\=\=\s^[A-Z_]+$", i):
        print "yyy"
        break

此处的数据是解析文件中的行列表。我写的表达式有什么错误?

如果要匹配仅由大写字母(可能由下划线分隔)组成的字符串,请使用:

^[A-Z]+(?:_[A-Z]+)*$
示例脚本:

inp = "EQUAL_NUM_SEQUENTIAL_LBAS"
if re.search(r'^[A-Z]+(?:_[A-Z]+)*$', inp):
    print "MATCH"
正则表达式模式,大声按顺序读出,表示匹配一些仅大写字母的单词,后跟下划线和另一个单词,零次或多次

要捕获出现在较大文本/文档中任何位置的此类词语,请使用:

inp = "Here is one ABC_DEF word and another EQUAL_NUM_SEQUENTIAL_LBAS here"
words = re.findall(r'\b[A-Z]+(?:_[A-Z]+)*\b', inp)
print(words)
这张照片是:

['EQUAL_NUM_SEQUENTIAL', 'LBAS']

删除模式中的^charector

 r"\s\s\=\=\s[A-Z_]+$"

试着删除
^
$
似乎你想要
\“
而不是
^
,你还需要用
\\”替换
$
:$
只是字符串起作用,但当我试图抓住整行时,它不起作用。我已经用一个选项更新了我的答案,以便在更大的文本中查找这些单词。