Python Regex不用多个单词(re)扭动
示例
import re
data = 'DisplayName = Umarex Steel Storm BB-Gun ,'
pattern = r'(\w+)\s*=\s*(\w+)'
re.findall(pattern, data, re.MULTILINE)
输出
错误结果:( displayName,umarex)
正确结果:( displayName,umarex store storm bb-gun)
Example
import re
data = 'DisplayName = Umarex Steel Storm BB-Gun ,'
pattern = r'(\w+)\s*=\s*(\w+)'
re.findall(pattern, data, re.MULTILINE)
Output
Wrong result: (DisplayName, Umarex)
Right result: (DisplayName, Umarex Steel Storm BB-Gun)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
regex demo 。
详细信息:
(\ w+)
- 组1:一个或多个字母,数字,连接器标点\ s*= \ s*= \ s*
- a <代码> = 用零或更多的whitespaces([^\ s,](?:[^,])*[^\ s,])?)
- 组2:A除了空格和逗号以外,其他任何零或更多非comma字符的可选序列,然后是白色空格和逗号以外的炭。You can use
See the regex demo.
Details:
(\w+)
- Group 1: one or more letters, digits, connector punctuation\s*=\s*
- a=
char enclosed with zero or more whitespaces([^\s,](?:[^,]*[^\s,])?)
- Group 2: a char other than whitespace and comma, then an optional sequence of any zero or more non-comma chars and then a char other than whitespace and comma.