2013-04-30 13 views
2

我想用另一个字符分割数字。Python的正则表达式:如何匹配一个数字与非数字?

输入:

we spend 100year 

输出:

we speed 100 year 

输入:

today i'm200 pound 

输出

today i'm 200 pound 

输入:

he maybe have212cm 

输出:

he maybe have 212 cm 

我试图re.sub(r'(?<=\S)\d', ' \d', string)re.sub(r'\d(?=\S)', '\d ', string),这是行不通的。

回答

5

这将做到这一点:

ins='''\ 
we spend 100year 
today i'm200 pound 
he maybe have212cm''' 

for line in ins.splitlines(): 
    line=re.sub(r'\s*(\d+)\s*',r' \1 ', line) 
    print line 

打印:

we spend 100 year 
today i'm 200 pound 
he maybe have 212 cm 

在同一文本行的多个匹配相同的语法:

>>> re.sub(r'\s*(\d+)\s*',r' \1 ', "we spend 100year + today i'm200 pound") 
"we spend 100 year + today i'm 200 pound" 

的捕获组(通常)被编号左到右和\number是指每个编号组在匹配中:

>>> re.sub(r'(\d)(\d)(\d)',r'\2\3\1','567') 
'675' 

如果它比较容易阅读,您可以命名捕获组而不是使用第e \1 \2表示法:

>>> line="we spend 100year today i'm200 pound" 
>>> re.sub(r'\s*(?P<nums>\d+)\s*',r' \g<nums> ',line) 
"we spend 100 year today i'm 200 pound" 
+0

d'oh!为什么我没有想到这一点。 +1 – mgilson 2013-04-30 02:38:44

+0

here \ 1是(\ d +)匹配结果。对? – NamNamNam 2013-04-30 02:43:33

+2

是的。有几件事要注意。正则表达式字符串开始处的'r'改变了这些字符串中'\'的含义,以使它们更易于阅读。捕获组括号被编号(通常)从左到右。所以如果你有're.sub(r'(\ d)(\ d)(\ d)',r'\ 2 \ 3 \ 1','123')'它会打印'231'你会得到它。 – dawg 2013-04-30 02:50:35

2

这需要照顾1案件:

>>> re.sub(r'([a-zA-Z])(?=\d)',r'\1 ',s) 
'he maybe have 212cm' 

而这需要其他的护理:

>>> re.sub(r'(?<=\d)([a-zA-Z])',r' \1',s) 
'he maybe have212 cm' 

希望有人比我更正则表达式的经验可以弄清楚如何将它们结合起来.. 。

+0

我使用两者。现在就工作! 你能解释为什么这项工作? 为什么\ 1工作? 谢谢! – NamNamNam 2013-04-30 02:34:34

+0

@NamNamNam - ''\ 1''匹配扩展到它遇到的第一个捕获组的结果。同样,'\ 2'会成为第二组等。等等。 – mgilson 2013-04-30 02:35:55

+0

再次感谢您!非常有帮助! – NamNamNam 2013-04-30 02:36:54

相关问题