拆分琴弦而无需删除分离器
我有以下文本,
text = "12345678 abcdefg 37394822 gdzdnhqihdzuiew 09089799 78998728 gdjewdwq"
我希望输出是:
12345678 abcdefg
37394822 gdzdnhqihdzuiew
09089799
78998728 gdjewdwq
我尝试了“ re.split(“ \ d {8}”,text)”,但结果不正确。 如何获得正确的输出?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用“ lookahead”
regex tutorial- lookahead- lookbehind and lookbehind Zere Lengthens Zere Length Spertions
You can use "Lookahead"
Regex Tutorial - Lookahead and Lookbehind Zero-Length Assertions
iiuc,您希望将数字部分与字母数字和数字配对,始终是每行的第一条问题,
而不是解决方案的优雅,而是解决问题
IIUC, you looking to pair the numeric part with the alphanumeric and numeric will always be the first on each line
not an elegant of solution but addresses the question
我更喜欢 @itagaki的答案,但值得注意的是,
findall
也可以使用:demo
正则表达式可以分解如下。
如果需要完全有8位数字,并且字符串小写字母的长度在(例如)7和15之间(如示例),则将其正则稍微修改:
I prefer @Itagaki's answer but it's worth noting that
findall
could also be used:Demo
The regular expression can be broken down as follows.
If it were required that there be exactly 8 digits and that the strings lowercase letters have lengths between (say) 7 and 15 (as in the example), the regex would be modified slightly:
如果要匹配8位数字,则可以使用:
说明
\ b \ d {8} \ b
匹配8位被单词边界包围的数字以防止部分匹配。
\ s*
匹配可选的Whitespace Chars(?:\ b \ d {8} \ b | $)
匹配8位或断言字符串的结尾(?:\ b )
关闭lookaheadRegex Demo | python demo
示例
If you want to match 8 digits, you can use:
Explanation
\b\d{8}\b
Match 8 digits surrounded by word boundaries to prevent partial matches.*?
Match any char, as least as possible(?=
Positive lookahead\s*
Match optional whitespace chars(?:\b\d{8}\b|$)
Match either 8 digits or assert the end of the string)
Close lookaheadRegex demo | Python demo
Example
Output