本文介绍了正则表达式 - 带空格和小数点逗号的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Pyhton 中为以下类型的字符串编写一个正则表达式:

1 100

1 567 865

1 474 388 346

即数字与千分开.这是我的正则表达式:

r"(\d{1,3}(?:\s*\d{3})*)

而且它工作正常.但是,我也想解析

1 100,34848

1 100 300,8

19 328 383 334,23499

即用十进制数字分隔的数字.我写了

rr=r"(\d{1,3}(?:\s*\d{3})*)(,\d+)?\s

它不起作用.例如,如果我使

sentence = "jsjs 2 222,11 dhd"

re.findall(rr, 句子)

[('2 222', ',11')]

感谢任何帮助,谢谢.

解决方案

结果的唯一问题是您得到两个匹配组而不是一个.发生这种情况的唯一原因是您正在创建两个捕获组而不是一个.您在前半部分和后半部分分别放置了括号,这就是括号的含义.只是不要那样做,你就不会有那个问题.

所以,有了这个,你就成功了:

(\d{1,3}(?:\s*\d{3})*,\d+)\s

I'd like to write a regular expression for following type of strings in Pyhton:

i.e. numbers separated from thousand. Here's my regexp:

and it works fine. However, I also wanna parse

i.e. separated numbers with decimal digits. I wrote

It doesn't work. For instance, if I make

Any help appreciated, thanks.

解决方案

The only problem with your result is that you're getting two match groups instead of one. The only reason that's happening is that you're creating two capture groups instead of one. You're putting separate parentheses around the first half and the second half, and that's what parentheses mean. Just don't do that, and you won't have that problem.

So, with this, you're half-way there:

(\d{1,3}(?:\s*\d{3})*,\d+)\s

Debuggex Demo

The only problem is that the ,\d+ part is now mandatory instead of optional. You obviously need somewhere to put the ?, as you were doing. But without a group, how do you do that? Simple: you can use a group, just make it a non-capturing group ((?:…) instead of (…)). And put it inside the main capturing group, not separate from it. Exactly as you're already doing for the repeated \s*\d{3} part.

(\d{1,3}(?:\s*\d{3})*(?:,\d+)?)\s

Debuggex Demo

这篇关于正则表达式 - 带空格和小数点逗号的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 03:03
查看更多