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

正则表达式问题,python

正则表达式问题,python,python,regex,Python,Regex,我正在尝试编写一个用于解析器的regexp。具体来说,我希望能够解析如下格式的字符串: [SOME-WORD "Quoted string"] 目前,我正在尝试以下表达式: (?P<capital-item> \[SOME-WORD(\ ?)\" (?P<quoted-string>\w+) \"(\ ?)\]) (?P \[一些单词(\?)\” (?P\w+) \"(\ ?)\]) 我正在使用python,并重新编译以获得扫描仪。编译后,regexp与我上面给

我正在尝试编写一个用于解析器的regexp。具体来说,我希望能够解析如下格式的字符串:

[SOME-WORD "Quoted string"]
目前,我正在尝试以下表达式:

(?P<capital-item>
\[SOME-WORD(\ ?)\"
  (?P<quoted-string>\w+)
\"(\ ?)\])
(?P
\[一些单词(\?)\”
(?P\w+)
\"(\ ?)\])
我正在使用python,并重新编译以获得扫描仪。编译后,regexp与我上面给出的示例字符串不匹配。我在这里搞砸了什么?

>>import re
>>> import re
>>> text = '[SOME-WORD "Quoted string"]'
>>> pat = r'\[(?P<capitalitem>SOME-WORD)(\ ?)\"(?P<quotedstring>[\w\s]+)\"(\ ?)\]'
>>> re.search(pat, text).groupdict()
{'capitalitem': 'SOME-WORD', 'quotedstring': 'Quoted string'}
>>>text='[SOME-WORD“Quoted string”]' >>>pat=r'\[(?PSOME-WORD)(\?)\”(?P[\w\s]+)\“(\?)\]” >>>检索(pat,text).groupdict() {'capitalitem':'SOME-WORD','quotedstring':'quotedstring'}
>>导入re
>>>text='[SOME-WORD“Quoted string”]'
>>>pat=r'\[(?PSOME-WORD)(\?)\”(?P[\w\s]+)\“(\?)\]”
>>>检索(pat,text).groupdict()
{'capitalitem':'SOME-WORD','quotedstring':'quotedstring'}

对不起,输入错误。不是这样的。
\w
与“Quoted”和“string”之间的空格不匹配<代码>\w相当于
[a-zA-Z0-9\]
(无空格)。组名必须是有效的Python标识符。
大写项目
带引号的字符串
都不是。您希望从中得到什么?一个示例结果会很好。“某个单词”是静态的吗?对不起,输入错误。不是这样的。
\w
与“Quoted”和“string”之间的空格不匹配<代码>\w相当于
[a-zA-Z0-9\]
(无空格)。组名必须是有效的Python标识符。
大写项目
带引号的字符串
都不是。您希望从中得到什么?一个示例结果会很好。“某个词”是这个的静态部分吗?