正则表达式提取未知数格式的负数

时间:2018-07-08 14:08:15

标签: python regex

我可以从字符串中获取数字:

import re def extractVal2(s,n): if n > 0: return re.findall(r'[0-9$,.%]+\d*', s)[n-1] else: return re.findall(r'[0-9$,.%]+\d*', s)[n] for i in range(1,7): print extractVal2(string_n,i)

使用以下代码:

string_n= 'seven (5) blah (6)  decimal (6.5) thousands (8,999) with dollar signs $(9,000) and $(9,500,001.45) end lastly.... (8.4)% now end'

但是我不能用它做负数。负数是括号中的数字。

()

我尝试先用负号替换string_n= re.sub(r"\((\d*,?\d*)\)", r"-\1", string_n)

r'[0-9$,.%-]+\d*', s)[n] r'[0-9$,.%]+-\d*', s)[n] r'[-0-9$,.%]+-\d*', s)[n]

然后得到负数

words = string_n.split(" ")
for i in words:
    try:
        print -int(i.translate(None,"(),"))
    except:
        pass

甚至使用其他方法:

var items = [
{
    name: 'dell-66',
    price: 200,
    id: 12,
},
{
    name: 'hp-44',
    price: 100,
    id: 10,
},
{
    name: 'acer-33',
    price: 250,
    id: 33,
},
{
    name: 'dell-66',
    price: 200,
    id: 12,
},
{
    name: 'acer-33',
    price: 250,
    id: 33,
},
{
    name: 'dell-66',
    price: 200,
    id: 12,
},
]

var rlt = {};

items.map(item => {
  let price = item.price
  let name = item.name
  if (rlt[name]) {
    rlt[name] = rlt[name] + price
  } else {
    rlt[name] = price;
  }
});
console.log(rlt);

1 个答案:

答案 0 :(得分:3)

您可以将正则表达式更改为此:

import re

def extractVal2(s,n):
    try:
        pattern = r'\$?\(?[0-9][0-9,.]*\)?%?'
        if n > 0:
            return re.findall(pattern, s)[n-1].replace("(","-").replace(")","")
        else:
            return re.findall(pattern, s)[n].replace("(","-").replace(")","")
    except IndexError as e:
        return None    

string_n=  ',seven (5) blah (6) decimal (6.5) thousands (8,999) with dollar ' + \
           'signs $(9,000) and $(9,500,001.45) end lastly.... (8.4)%'

for i in range(1,9): 
    print extractVal2(string_n,i)

它也将解析9,500,001.45-捕获(之后和数字之前的前导$,并用-符号代替。不过,这是一个骇客-它不会“查看”您的(是否没有),并且还会捕获“ 2,200.200,22”之类的“非法”数字。

输出:

-5
-6
-6.5
-8,999
$-9,000
$-9,500,001.45
-8.4%
None

如果您的IndexError没有捕获任何内容(或捕获的内容太少),并且您正在索引返回的列表之后,您也应该考虑捕获re.findall(..)


正则表达式允许:

leading literal $       (not interpreded as ^...$ end of string)
optional literal (  
[0-9]                   one digit
[0-9,.%]*               any number (maybe 0 times) of the included characters in any order  
                        to the extend that it would mach smth like 000,9.34,2
optional literal )
optional literal %