9184

regex pattern to match datetime in python

<h3>Question</h3>

I have a string contains datetimes, I am trying to split the string based on the datetime occurances,

data="2018-03-14 06:08:18, he went on \n2018-03-15 06:08:18, lets play"

what I am doing,

out=re.split('^(2[0-3]|[01]?[0-9]):([0-5]?[0-9]):([0-5]?[0-9])$',data)

what I get

["2018-03-14 06:08:18, he went on 2018-03-15 06:08:18, lets play"]

What I want:

["2018-03-14 06:08:18, he went on","2018-03-15 06:08:18, lets play"]
<h3>Answer1:</h3>

You want to split with at least 1 whitespace followed with a date like pattern, thus, you may use

re.split(r'\s+(?=\d{2}(?:\d{2})?-\d{1,2}-\d{1,2}\b)', s)

See the regex demo

Details

<ul><li>\s+ - 1+ whitespace chars</li> <li>(?=\d{2}(?:\d{2})?-\d{1,2}-\d{1,2}\b) - a <em>positive lookahead</em> that makes sure, that immediately to the left of the current location, there are <ul><li>\d{2}(?:\d{2})? - 2 or 4 digits</li> <li>- - a hyphen</li> <li>\d{1,2} - 1 or 2 digits</li> <li>-\d{1,2} - again a hyphen and 1 or 2 digits</li> <li>\b - a word boundary (if not necessary, remove it, or replace with (?!\d) in case you may have dates glued to letters or other text)</li> </ul></li> </ul>

Python demo:

import re rex = r"\s+(?=\d{2}(?:\d{2})?-\d{1,2}-\d{1,2}\b)" s = "2018-03-14 06:08:18, he went on 2018-03-15 06:08:18, lets play" print(re.split(rex, s)) # => ['2018-03-14 06:08:18, he went on', '2018-03-15 06:08:18, lets play']

NOTE If there can be no whitespace before the date, in Python 3.7 and newer you may use r"\s*(?=\d{2}(?:\d{2})?-\d{1,2}-\d{1,2}\b)" (note the * quantifier with \s* that will allow zero-length matches). For older versions, you will need to use a solution as @blhsing suggests or install PyPi regex module and use r"(?V1)\s*(?=\d{2}(?:\d{2})?-\d{1,2}-\d{1,2}\b)" with regex.split.


<h3>Answer2:</h3>

re.split is meant for cases where you have a certain delimiter pattern. Use re.findall with a lookahead pattern instead:

import re data="2018-03-14 06:08:18, he went on \n2018-03-15 06:08:18, lets play" d = r'\d{4}-\d?\d-\d?\d (?:2[0-3]|[01]?[0-9]):[0-5]?[0-9]:[0-5]?[0-9]' print(re.findall(r'{0}.*?(?=\s*{0}|$)'.format(d), data, re.DOTALL))

This outputs:

['2018-03-14 06:08:18, he went on', '2018-03-15 06:08:18, lets play']
<h3>Answer3:</h3>

An similar, but alternative solution using a group instead:

import re data="2018-03-14 06:08:18, he went on 2018-03-15 06:08:18, lets play" print(re.findall(r'(.*?\D{2,})', data))

Which gives:

['2018-03-14 06:08:18, he went on ', '2018-03-15 06:08:18, lets play']

来源:https://stackoverflow.com/questions/51395590/regex-pattern-to-match-datetime-in-python

Recommend

  • Richfaces 4 rich:inputNumberSpinner: not getting “set” in backing bean
  • Cannot replace default icon in android studio
  • How to serialize a JObject the same way as an object with Json.NET?
  • what's best way to check if a S3 object exists?
  • rgdal doesn't recognize GDAL version
  • Draw common friends connections of three people using networkx
  • Adjust View for keyboard appears when switching UITextField (Swift)
  • jQuery 'click' event doesn't work on iOS Safari?
  • sbt-scalariform plugin - can't resolve settings
  • R: Creating a new column using another dataframe
  • Initializing a pointer to compound literals in C
  • Modification of innerHTML stored in variable not working
  • Git objects SHA-1 are file contents or file names?
  • Error: packet sequence number wrong after upgrading RDS instance
  • SQL Merge 3 tables by date where some dates are missing
  • Microsoft bot framework webchat C#
  • Lodash cloneDeepWith to omit undefined
  • How to create wsdl from xsd
  • Calculate savings percentage for house down payment in 36 months
  • How to wrap a JMS to WebSphere MQ bridge in a synchronous call using the request-reply pattern?
  • date changes on export kendoGrid
  • mssql script data insert or update
  • jQuery colorbox breaks postbacks in ASP.NET Web Forms
  • opencv deskewing a contour
  • What does “T extends Junk” mean in a generic class in Java?
  • Facebook Error (#200) The user hasn't authorized the application to perform this action (PHP)
  • ASP.NET MVC 2 actions for the same route?
  • How do I use libcurl to printf a remote FTP directory listing?
  • gnuplot - How to make zmin equal to zmax keeeping autoscale on z axis
  • How to make 100% div height between header and footer?
  • Error handeling in antlr 3.0
  • How to clear a browser cache in Protractor
  • Drag and drop unicode TText in DelphiXe4
  • Terminal run dalvikvm with am.jar
  • Angular FormGroup won't update it's value immediately after patchValue or setValue
  • ReferenceError: TextEncoder is not defined
  • ARKit code issue {unknown error -1=ffffffffffffffff error: Task failed with exit 1}