Python Regex不用多个单词(re)扭动

发布于 2025-02-09 19:47:45 字数 324 浏览 0 评论 0原文

示例

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昔梦 2025-02-16 19:47:45

您可以使用

(\w+)\s*=\s*([^\s,](?:[^,]*[^\s,])?)

regex demo

详细信息

  • (\ w+) - 组1:一个或多个字母,数字,连接器标点
  • \ s*= \ s*= \ s* - a <代码> = 用零或更多的whitespaces
  • ([^\ s,](?:[^,])*[^\ s,])?) - 组2:A除了空格和逗号以外,其他任何零或更多非comma字符的可选序列,然后是白色空格和逗号以外的炭。

You can use

(\w+)\s*=\s*([^\s,](?:[^,]*[^\s,])?)

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文