使用正则表达式在python中使用标签提取字符串

问题描述 投票:0回答:3

我想从python中的文本数据中提取带有标签的字符串。我已经编写了以下代码,但这会用字符串替换实际数据,我想提取

    import re
def replace_entities(example):

# dd mm yyyy
example = re.sub("(\d{1,31}(:? |\-|\/)\d{1,12}(:? |\-|\/)\d{4})", "DATESTR", example)  # dd/mm/yyyy
example = re.sub("(\d{4}(:? |\-|\/)\d{1,31}(:? |\-|\/)\d{1,12})", "DATESTR", example)  # yyyy/dd/mm

# email id
example = re.sub("[\w\.-]+@[\w\.-]+", "EMAILIDSTR", example)

# URL
example = re.sub('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', "URLSTR",
                    example)
example = re.sub('www.(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', "URLSTR", example)

# TIME
example = re.sub("\d{2}:\d{2} (:?AM|PM|am|pm)", "TIMESTR", example)
example = re.sub("\d{2}:\d{2}:\d{3} (:?AM|PM|am|pm)", "TIMESTR", example)

# MONEY
example = re.sub(r'\£ \d+', "MONEYSTR", example, 0)
example = re.sub(r'\£\d+', "MONEYSTR", example, 0)
example = re.sub(r'\d+(:?\£|pound|pounds|euros|euro)', "MONEYSTR", example, 0)
example = re.sub(r'\d+ (:?\£|pound|pounds|euros|euro)', "MONEYSTR", example, 0)
example = re.sub(r'\d.\d+(:?\£|pound|pounds|euros|euro)', "MONEYSTR", example, 0)
example = re.sub(r'\d.\d+ (:?\£|pound|pounds|euros|euro)', "MONEYSTR", example, 0)
example = re.sub(r'\\xc2\\xa\d+', "MONEYSTR", example, 0)
example = re.sub(r'\\xc2\\xa\d+.\d+', "MONEYSTR", example, 0)

# Split alpha numeric and sp. symbol
example = " ".join(re.findall(r"[^,.:;\(\)\/\\_]+|[,.:;\(\)\/\\_]", example))
example = " ".join(re.findall(r"[^\d_]+|\d+", example))
example = re.sub('(?!^)([A-Z][a-z]+)', r' \1', example)

# NUMBERS
example = re.sub(r'\d+', 'NUMSTR', example)

return example

我有以下文字作为输入:

 My name is ali, Date is 21/08/2018 Total amount is euros 10 . Account number is 123456

Expected_output是:

> 21/08/2018: DATESTR
  euros 10 : MONEYSTR
  123456  :  NUMSTR

我怎样才能获得以上输出

有任何想法吗?

python regex
3个回答
2
投票

您可以通过添加之前的.*?.*之后的模式修复它,并替换为r'\1 : DATESTR'

 res = re.sub(r'.*?(\d{1,31}(?::? |[-/])\d{1,12}(?::? |[-/])\d{4}).*', r'\1 : DATESTR', s)

regex demo。使用.*?,您可以匹配除了换行符之外的任何0+字符,尽可能少,并且使用.*,您可以尽可能多地匹配换行符之外的任何0+字符,这样您就可以删除不需要的字符串只是匹配,你保持你捕获的东西。

您也可以使用正则表达式提取日期,然后将: DATESTR附加到它:

import re
rx = r"\d{1,31}(?::? |[-/])\d{1,12}(?::? |[-/])\d{4}"
s = "My name is ALi Date is 09/03/2018"
m = re.search(rx, s)
if m:
    print("{} : DATESTR".format(m.group())) # => 09/03/2018 : DATESTR

Python demo


0
投票

你可以试试datefinder 在这里,我试图用它完成你的例子:

>>> import datefinder
>>> str = 'My name is ALi Date is 09/03/2018'
>>> matches = datefinder.find_dates(str)
>>> for i in matches:
...     print(i.strftime("%m/%d/%Y") + ':DATESTR')
...
09/03/2018:DATESTR

我想这会对你有所帮助。它可以从字符串中获取任何日期字符串。


0
投票

从你的例子中你想要做两件事:

  1. 找一个类似日期的字符串
  2. 在比赛结束时添加另一个字符串

我在这里提出的解决方案可能不是最好的,但确实如此。我建议你得到正则表达式可以找到的匹配,然后使用该匹配来格式化你想要打印的内容。

import re

string1 = "My name is ALi Date is 09/03/2018"
string2 = "DATESTR"
m = re.search("(\d{1,31}(:? |\-|\/)\d{1,12}(:? |\-|\/)\d{4})", string1 ) # match the date : dd/mm/yyyy
print( m.group(0) + ' : ' + string2 )

输出是:

>>> 09/03/2018 : DATESTR

可能还有一些其他功能符合您在文档中的需求。这就是我刚刚用过的东西。

https://docs.python.org/3/library/re.html

© www.soinside.com 2019 - 2024. All rights reserved.