简体   繁体   English

为什么以下正则表达式在Python中不起作用?

[英]Why doesn't the following regular expression work in Python?

I have the following code: 我有以下代码:

regularexpression = r'([-\w]*\w)? ?: ?([-"\#\w\s_]*\w?);'
outputfr = re.findall(regularexpression, inputdata, re.IGNORECASE)
return data

It's supposed to catch words, hyphens and other characters, ending in ";". 应该捕获以“;”结尾的单词,连字符和其他字符。 So: 所以:

(hello-nine: hello, six, seven; hello-five: six eight) would output as [('hello-nine', 'hello, six, seven'), ('hello-five', 'six eight') (hello-nine: hello, six, seven; hello-five: six eight)将输出为[('hello-nine','hello,六个,七个'),('hello-五个','六个八')

If final-number: "seventy", "sixty", "fifty", forty is part of the user input (inputdata), regularexpression doesn't catch it. 如果final-number: "seventy", "sixty", "fifty", forty sentiy final-number: "seventy", "sixty", "fifty", forty是用户输入(输入数据)的一部分,则regularexpression不会捕获它。 I'd want it to output as [('final-number', '"seventy", "sixty", "fifty", "forty")] 我希望它输出为[('final-number', '"seventy", "sixty", "fifty", "forty")]

Why is this? 为什么是这样?

In your regular expression, the second group: 在您的正则表达式中,第二组:

([-"\#\w\s_]*\w?)

needs to be changed so that it will match commas: 需要进行更改,以使其与逗号匹配:

([-"\#\w\s_,]*\w?)

Your example inputs -> outputs are not consistent. 您的示例输入->输出不一致。 In the first case, the comma-separated items are kept together but in the second they are separate list elements. 在第一种情况下,逗号分隔的项目保持在一起,但是在第二种情况下,它们是单独的列表元素。 Also, do you want to strip parentheses? 另外,您是否要删除括号? quote marks? 引号? Clarify by giving actual values for inputdata and showing what exactly you want to return (including stripping quote marks, parentheses). 通过给予实际值澄清inputdata并显示正是你想要返回(包括剥离引号,括号中)是什么。 The data variable is never assigned. 永远不会分配data变量。

Using .split(";") might be a better starting point... 使用.split(";")可能是一个更好的起点...

inputdata = "(hello-nine: hello, six, seven; hello-five: six eight)"
mylist = inputdata.split(";")
# here either use regexp or another split, depending on what you want...
subset = [x.split(":") for x in mylist]

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

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