Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use regular expressions in python to get the date and time in a string

Shulou Source: shulou.com Published: 2022-06-02 14:26:26 10月03日 Update

This article shows you how to use regular expressions in python to get the date and time in a string. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Processing before extraction date

1. Unifying the date format for dealing with text data

Text = "on August 31, 2015, an object attack accident occurred in the finished product warehouse of Quzhou Yuanli Metal products Co., Ltd. (hereinafter referred to as Yuanli Storage Company), resulting in a direct economic loss of 950000 yuan." Text1 = "Administrative penalty decision issued on 2015-12-28" text2 = "an object strike accident occurred in August 2015" # deal with the text # 2015-8-31 2015-12-28text = text.replace ("year", "-"). Replace ("month", "-"). Replace ("day", "). Replace (" / ","-"). Strip ()

two。 Regular expression for extracting time

# Oct 27th 2019 9:46:21 "(\ d {4} -\ d {1recov 2} -\ d {1rem 2}:\ d {1pr 2}:\ d {1pr 2}:\ d {1pr 2})" # Oct 27th 2019 "(\ d {4} -\ d {1pm 2} -\ d {1cr 2})" # Oct 2019 "(\ d {4} -\ d {1j 2})" # Oct 2019 "(\ d {4} -\ d {1j 2})"

3. Encapsulate it

Def get_strtime (text): text = text.replace ("year", "-"). Replace ("month", "-"). Replace ("day", "). Replace (" / ","-"). Strip () text = re.sub ("\ s + "," Text) t = "regex_list = [# # 22:46:21 on August 15, 2013" (\ d {4} -\ d {1meme2} -\ d {1meme2}:\ d {1Power2}:\ d {1Power2}:\ d {1flint 2}), # "22:46 on August 15, 2013" (\ d {4} -\ d {1cr 2} -\ d {1cr 2}:\ d {1pm 2}) " # for regex in regex_list: t = re.search (regex, text) if t: t = t.group (1) return t else: print ("effective date not obtained")

Ps: let's take a look at the date in the python extraction string

Import re# deletes the Chinese characters def subChar (str): match=re.compile (u' [\ u4e00 -\ u9fa5]') return match.sub (', str) # extraction date def extractDate (str): if not str: return None raw=subChar (str) if not raw: return None # extract the first 10 characters rawdate=raw [: 10] datelist=re.findall ("\ d +") Rawdate) if not datelist: return None if datelist.__len__ () = = 3: if (float (datelist [0]) > 2099 or float (datelist [0]) 12 or float (datelist [2]) > 31: return None else: return'- '.join (datelist) if datelist.__len__ () = = 2: if (float (datelist [0]) > 2099 or float (datelist [0]) 12: return None else: Datelist.append ('01') return'-'.join (datelist) if datelist.__len__ () = = 1: if float (datelist [0]) > 20991231 or float (datelist [0])

Tags: Date character string time regular expression company processing accident content skills text object knowledge warehousing valid concise concise warehouse hereinafter referred to as Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Redmi Docker NVidia Shulou Technology