如何标记,分割相邻的数字文字?

2024-04-18 15:52:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将hello world123这样的东西标记为helloworld123。 我认为代码中有两部分是必需的,但不能将它们正确地组合成tokenize。你知道吗

(?u)\b\w+\b
(?<=\D)(?=\d)|(?<=\d)(?=\D)

Tags: 代码标记helloworldtokenizeworld123
1条回答
网友
1楼 · 发布于 2024-04-18 15:52:09

你可以用

import re
s = "hello world123"
print(re.findall(r'[^\W\d_]+|\d+', s))
# => ['hello', 'world', '123']

参见Python demo

图案细节

  • [^\W\d_]+-1个或多个字母
  • |-或
  • \d+-1+个数字。你知道吗

参见regex demo。你知道吗

奖励:匹配任何字母子串和各种数字

[^\W\d_]+|[-+]?\d*\.?\d+(?:[eE][+-]?\d+)?

this regex demo。你知道吗

有关正则表达式的详细信息,请参见Parsing scientific notation sensibly?。你知道吗

相关问题 更多 >