简体   繁体   English

从字符串中提取数字,同时保留空格

[英]Extract numbers from a string while maintaining the whitespaces

I have some string like this 我有一些这样的字符串

'    12    2    89   29   11    92     92     10'

(all the numbers are positive integers so no - and no . ), and I want to extract all numbers from it, edit some of the numbers, and then put them all together with the same whitespaces. (所有的数字都是正整数所以没-没有. ),我想从中提取所有的数字,编辑一些数字,然后把它们都具有相同的空格在一起。 For example, if I change the number 11 to 22 , I want the final string as 例如,如果我将数字11更改为22 ,我希望最后一个字符串为

'    12    2    89   29   22    92     92     10'

I did some search and most questions disregard the whitespaces and only care about the numbers. 我进行了一些搜索,大多数问题都忽略了空格,只关心数字。 I tried 我试过了

match = re.match((\s*(\d+)){8}, str)

but match.group(0) gives me the whole string,, match.group(1) gives me the first match \\ 12 (I added the \\ otherwise the website won't show the leading whitespaces), and match.group(2) gives me 12 . 但是match.group(0)给了我整个字符串, match.group(1)给了我第一个匹配\\ 12 (我加了\\否则网站将不会显示前导空格),以及match.group(2)给我12 But it won't give me any numbers after that, any index higher than 2 gives me an error. 但是之后它不会给我任何数字,任何大于2索引都会给我一个错误。 I don't think my approach is the correct one, what is the right way to do this? 我认为我的方法不是正确的方法,正确的方法是什么?

I just tried re.split('(\\d+)', str) and that seems to be what I need. 我只是试过re.split('(\\d+)', str) ,这似乎是我所需要的。

I'd recommend using a regular expression with non-capturing groups , to get a list of 'space' parts and 'number' parts: 我建议使用带有非捕获组的正则表达式,以获取“空格”部分和“数字”部分的列表:

In [15]: text = '    12    2    89   29   11    92     92     10'
In [16]: parts = re.findall('((?: +)|(?:[0-9]+))', text)
In [17]: parts
Out[17]: ['    ', '12', '    ', '2', '    ', '89', '   ', '29', '   ',
  '11', '    ', '92', '     ', '92', '     ', '10']

Then you can do: 然后,您可以执行以下操作:

for index, part in enumerate(parts):
    if part == '11':
        parts[index] = '22'
replaced = ''.join(parts)

(or whatever match and replacement you want to do). (或您要进行的任何匹配和替换)。

Match all numbers with spaces, change desired number and join array. 将所有数字与空格匹配,更改所需数字并加入数组。

import re

newNum = '125'
text = '    12    2    89   29   11    92     92     10'
                                              ^^
marray = re.findall(r'\s+\d+', text)
marray[6] = re.sub(r'\d+', newNum, marray[6])

print(marray) 

['    12', '    2', '    89', '   29', '   11', '    92', '     125', '     10']

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

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