2024-04-18 15:52:09 发布
网友
我试图将hello world123这样的东西标记为hello、world和123。 我认为代码中有两部分是必需的,但不能将它们正确地组合成tokenize。你知道吗
hello world123
hello
world
123
tokenize
(?u)\b\w+\b (?<=\D)(?=\d)|(?<=\d)(?=\D)
你可以用
import re s = "hello world123" print(re.findall(r'[^\W\d_]+|\d+', s)) # => ['hello', 'world', '123']
参见Python demo
图案细节
[^\W\d_]+
|
\d+
参见regex demo。你知道吗
奖励:匹配任何字母子串和各种数字
[^\W\d_]+|[-+]?\d*\.?\d+(?:[eE][+-]?\d+)?
见this regex demo。你知道吗
有关正则表达式的详细信息,请参见Parsing scientific notation sensibly?。你知道吗
你可以用
参见Python demo
图案细节
[^\W\d_]+
-1个或多个字母|
-或\d+
-1+个数字。你知道吗参见regex demo。你知道吗
奖励:匹配任何字母子串和各种数字
见this regex demo。你知道吗
有关正则表达式的详细信息,请参见Parsing scientific notation sensibly?。你知道吗
相关问题 更多 >
编程相关推荐