简体   繁体   English

Python正则表达式:如何将数字与非数字匹配?

[英]Python regex: How to match a number with a non-number?

I want split number with another character. 我希望拆分号码与另一个角色。

Example

Input: 输入:

we spend 100year

Output: 输出:

we speed 100 year

Input: 输入:

today i'm200 pound

Output 产量

today i'm 200 pound

Input: 输入:

he maybe have212cm

Output: 输出:

he maybe have 212 cm

I tried re.sub(r'(?<=\\S)\\d', ' \\d', string) and re.sub(r'\\d(?=\\S)', '\\d ', string) , which doesn't work. 我试过re.sub(r'(?<=\\S)\\d', ' \\d', string)re.sub(r'\\d(?=\\S)', '\\d ', string) ,这不起作用。

This will do it: 这样做:

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

Prints: 打印:

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

Same syntax for multiple matches in the same line of text: 同一行文本中多个匹配项的语法相同:

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

The capturing groups (generally) are numbered left to right and the \\number refers to each numbered group in the match: 捕获组(通常)从左到右编号, \\number表示匹配中的每个编号组:

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

If it is easier to read, you can name your capturing groups rather than using the \\1 \\2 notation: 如果它更容易阅读,您可以命名您的捕获组而不是使用\\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"

This takes care of one case: 这照顾一种情况:

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

And this takes care of the other: 这会照顾到另一个:

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

Hopefully someone with more regex experience than me can figure out how to combine them ... 希望比我有更多正则表达式经验的人可以弄清楚如何将它们组合在一起...

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

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