如何将字符串内部映射到不包含数字正则表达式的引号

时间:2017-10-28 23:00:52

标签: regex python-2.7 python-3.x

我的字符串如下:

  • “记录一”,“字段a”:“随机a b c”,“提交b”:“随机a b c”,“提交 c“:”123“
  • “记录2”,“字段a”:“随机a b c”,“提交b”:“随机a b c“,”提交c“:”abc“

如何使用正则表达式从不包含任何数字的字段c中提取值?我试过像:

r"\"filed c\"\:\"([^\"\d]*)"

但它没有用。谢谢。

1 个答案:

答案 0 :(得分:0)

说你有,

records = [
    '"record one","field a":"random a b c","filed b":"random a b c","filed c":"123"',
    '"record two","field a":"random a b c","filed b":"random a b c","filed c":"abc"',
    '"record ...","field a":"random a b c","filed b":"random a b c","filed c":"ab12c"',
    '"record ...","field a":"random a b c","filed b":"random a b c","filed c":""',
    '"record ...","field a":"random a b c","filed b":"random a b c","filed c":"12abc"',
    '"record nth","field a":"random a b c","filed b":"random a b c","filed c":"Xyz"',
]

做什么

import re
rex = '"filed c":"([^\d]*)"'
does_not_contain_any_number = []
for record in records:
    q = re.compile(rex, re.DOTALL).findall(record)
    if len(''.join(q)):
        does_not_contain_any_number += q


print(does_not_contain_any_number)
#['abc', 'Xyz']