简体   繁体   English

Python RegEx帮助:将字符串拆分为数字,字符和空格

[英]Python RegEx help: Splitting string into numbers, characters and whitespace

I am trying to split my string into a list, separating by whitespace and characters but leaving numbers together. 我试图将我的字符串分成一个列表,用空格和字符分隔,但将数字放在一起。
For example, the string: 例如,字符串:

"1 2 +="  

would end up as: 最终会变成:

["1", " ", "2", " " ,"+", "="]    

The code I currently have is 我目前拥有的代码是

temp = re.findall('\d+|\S', input)  

This seperates the string as intended but does also remove the whitespace, how do I stop this? 这样可以按预期分隔字符串,但同时也删除了空格,如何停止呢?

Just add \\s or \\s+ to your current regular expression (use \\s+ if you want consecutive whitespace characters to be grouped together). 只需将\\s\\s+添加到当前的正则表达式中即可(如果要将连续的空白字符分组在一起,请使用\\s+ )。 For example: 例如:

>>> s = "1 2 +="
>>> re.findall(r'\d+|\S|\s+', s)
['1', ' ', '2', ' ', '+', '=']

If you don't want consecutive whitespace to be grouped together, then instead of r'\\d+|\\S|\\s' it would probably make more sense to use r'\\d+|\\D' . 如果您不希望将连续的空白分组在一起,那么使用r'\\d+|\\D'代替r'\\d+|\\S|\\s'可能更有意义。

You can use \\D to find anything that is not a digit: 您可以使用\\D查找不是数字​​的任何内容:

\d+|\D

Python: 蟒蛇:

temp = re.findall(r'\d+|\D', input) 
//Output: ['1', ' ', '2', ' ', '+', '=']

It would also work if you just used . 如果您刚刚使用它也将起作用. since it'll match the \\d+ first anyway. 因为无论如何它都会先匹配\\d+ But its probably cleaner not to. 但是它可能更清洁。

\d+|.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM