Writing regex to capture many different date formats
I'm trying to write a regular expression which will capture the following time formats:
H:MM
HH:MM
as well as variations with P.M or PM right after or with a space after the time, or in lower case, or with AM instead (hope you get the idea). As some examples:
1:00PM, 1:00pm, 1pm, 1PM, 1:00 PM, 1:00pm, 2:00
This is the regular expression I currently have:
([0-9]|0[0-9]|1[0-9]|2[0-3])(:[0-5][0-9])?(( )?(PM|AM|am|pm|a.m|p.m|P.M|P.M|a.m.|p.m.|P.M.|A.M.))?
However while working for all my formats, it also captures every single other integer and I don't know how to fix that.
To combat this I've written 2 separate ones, one which captures just the time:
([0-9]|0[0-9]|1[0-9]|2[0-3])(:[0-5][0-9])
and one which captures just the time with the PM/AM bit after:
([0-9]|0[0-9]|1[0-9]|2[0-3])(:[0-5][0-9])?(( )?(PM|AM|am|pm|a.m|p.m|P.M|P.M|a.m.|p.m.|P.M.|A.M.)){1,2}
but ideally I want one expression for them all, however all my attempts have resulted to a similar result to the regex at the start of this post.
python regex
|
show 2 more comments
I'm trying to write a regular expression which will capture the following time formats:
H:MM
HH:MM
as well as variations with P.M or PM right after or with a space after the time, or in lower case, or with AM instead (hope you get the idea). As some examples:
1:00PM, 1:00pm, 1pm, 1PM, 1:00 PM, 1:00pm, 2:00
This is the regular expression I currently have:
([0-9]|0[0-9]|1[0-9]|2[0-3])(:[0-5][0-9])?(( )?(PM|AM|am|pm|a.m|p.m|P.M|P.M|a.m.|p.m.|P.M.|A.M.))?
However while working for all my formats, it also captures every single other integer and I don't know how to fix that.
To combat this I've written 2 separate ones, one which captures just the time:
([0-9]|0[0-9]|1[0-9]|2[0-3])(:[0-5][0-9])
and one which captures just the time with the PM/AM bit after:
([0-9]|0[0-9]|1[0-9]|2[0-3])(:[0-5][0-9])?(( )?(PM|AM|am|pm|a.m|p.m|P.M|P.M|a.m.|p.m.|P.M.|A.M.)){1,2}
but ideally I want one expression for them all, however all my attempts have resulted to a similar result to the regex at the start of this post.
python regex
Welcome to StackOverflow. Your expression can be greatly simplified using regex flags. What programming language are you using?
– Dinei
Nov 21 '18 at 16:07
@Dinei it's in Python, I'll have a look at flags
– EchoTacticDouble
Nov 21 '18 at 16:08
Try this regex, I think it's simpler than what you have now. /^(0d|1d|2[0-3]|d)(:[0-5]d)?( *)?([AaPp].?[mM].?)?$/
– Alex G
Nov 21 '18 at 16:14
The.
character in your pattern (likea.m|p.m
) needs a "" character before (likea.m|p.m
)
– Foo
Nov 21 '18 at 16:17
@TânNguyễn That's a very good spot, need to change that
– EchoTacticDouble
Nov 21 '18 at 16:24
|
show 2 more comments
I'm trying to write a regular expression which will capture the following time formats:
H:MM
HH:MM
as well as variations with P.M or PM right after or with a space after the time, or in lower case, or with AM instead (hope you get the idea). As some examples:
1:00PM, 1:00pm, 1pm, 1PM, 1:00 PM, 1:00pm, 2:00
This is the regular expression I currently have:
([0-9]|0[0-9]|1[0-9]|2[0-3])(:[0-5][0-9])?(( )?(PM|AM|am|pm|a.m|p.m|P.M|P.M|a.m.|p.m.|P.M.|A.M.))?
However while working for all my formats, it also captures every single other integer and I don't know how to fix that.
To combat this I've written 2 separate ones, one which captures just the time:
([0-9]|0[0-9]|1[0-9]|2[0-3])(:[0-5][0-9])
and one which captures just the time with the PM/AM bit after:
([0-9]|0[0-9]|1[0-9]|2[0-3])(:[0-5][0-9])?(( )?(PM|AM|am|pm|a.m|p.m|P.M|P.M|a.m.|p.m.|P.M.|A.M.)){1,2}
but ideally I want one expression for them all, however all my attempts have resulted to a similar result to the regex at the start of this post.
python regex
I'm trying to write a regular expression which will capture the following time formats:
H:MM
HH:MM
as well as variations with P.M or PM right after or with a space after the time, or in lower case, or with AM instead (hope you get the idea). As some examples:
1:00PM, 1:00pm, 1pm, 1PM, 1:00 PM, 1:00pm, 2:00
This is the regular expression I currently have:
([0-9]|0[0-9]|1[0-9]|2[0-3])(:[0-5][0-9])?(( )?(PM|AM|am|pm|a.m|p.m|P.M|P.M|a.m.|p.m.|P.M.|A.M.))?
However while working for all my formats, it also captures every single other integer and I don't know how to fix that.
To combat this I've written 2 separate ones, one which captures just the time:
([0-9]|0[0-9]|1[0-9]|2[0-3])(:[0-5][0-9])
and one which captures just the time with the PM/AM bit after:
([0-9]|0[0-9]|1[0-9]|2[0-3])(:[0-5][0-9])?(( )?(PM|AM|am|pm|a.m|p.m|P.M|P.M|a.m.|p.m.|P.M.|A.M.)){1,2}
but ideally I want one expression for them all, however all my attempts have resulted to a similar result to the regex at the start of this post.
python regex
python regex
edited Nov 21 '18 at 16:13


Foo
1
1
asked Nov 21 '18 at 16:03
EchoTacticDoubleEchoTacticDouble
84
84
Welcome to StackOverflow. Your expression can be greatly simplified using regex flags. What programming language are you using?
– Dinei
Nov 21 '18 at 16:07
@Dinei it's in Python, I'll have a look at flags
– EchoTacticDouble
Nov 21 '18 at 16:08
Try this regex, I think it's simpler than what you have now. /^(0d|1d|2[0-3]|d)(:[0-5]d)?( *)?([AaPp].?[mM].?)?$/
– Alex G
Nov 21 '18 at 16:14
The.
character in your pattern (likea.m|p.m
) needs a "" character before (likea.m|p.m
)
– Foo
Nov 21 '18 at 16:17
@TânNguyễn That's a very good spot, need to change that
– EchoTacticDouble
Nov 21 '18 at 16:24
|
show 2 more comments
Welcome to StackOverflow. Your expression can be greatly simplified using regex flags. What programming language are you using?
– Dinei
Nov 21 '18 at 16:07
@Dinei it's in Python, I'll have a look at flags
– EchoTacticDouble
Nov 21 '18 at 16:08
Try this regex, I think it's simpler than what you have now. /^(0d|1d|2[0-3]|d)(:[0-5]d)?( *)?([AaPp].?[mM].?)?$/
– Alex G
Nov 21 '18 at 16:14
The.
character in your pattern (likea.m|p.m
) needs a "" character before (likea.m|p.m
)
– Foo
Nov 21 '18 at 16:17
@TânNguyễn That's a very good spot, need to change that
– EchoTacticDouble
Nov 21 '18 at 16:24
Welcome to StackOverflow. Your expression can be greatly simplified using regex flags. What programming language are you using?
– Dinei
Nov 21 '18 at 16:07
Welcome to StackOverflow. Your expression can be greatly simplified using regex flags. What programming language are you using?
– Dinei
Nov 21 '18 at 16:07
@Dinei it's in Python, I'll have a look at flags
– EchoTacticDouble
Nov 21 '18 at 16:08
@Dinei it's in Python, I'll have a look at flags
– EchoTacticDouble
Nov 21 '18 at 16:08
Try this regex, I think it's simpler than what you have now. /^(0d|1d|2[0-3]|d)(:[0-5]d)?( *)?([AaPp].?[mM].?)?$/
– Alex G
Nov 21 '18 at 16:14
Try this regex, I think it's simpler than what you have now. /^(0d|1d|2[0-3]|d)(:[0-5]d)?( *)?([AaPp].?[mM].?)?$/
– Alex G
Nov 21 '18 at 16:14
The
.
character in your pattern (like a.m|p.m
) needs a "" character before (like a.m|p.m
)– Foo
Nov 21 '18 at 16:17
The
.
character in your pattern (like a.m|p.m
) needs a "" character before (like a.m|p.m
)– Foo
Nov 21 '18 at 16:17
@TânNguyễn That's a very good spot, need to change that
– EchoTacticDouble
Nov 21 '18 at 16:24
@TânNguyễn That's a very good spot, need to change that
– EchoTacticDouble
Nov 21 '18 at 16:24
|
show 2 more comments
3 Answers
3
active
oldest
votes
You can try this regex in python: /((0d|1d|2[0-3]|d)(((:[0-5]d))|(s*[AaPp].?[mM].?))+)/
The first part matches the first part of the hour (HH or H), then next matches the minutes if any (:MM) and the last matches am, pm and their variances.
Demo: https://regex101.com/r/oPb7xb/2
This misses times such as 5:37, more concise than my solution though
– EchoTacticDouble
Nov 21 '18 at 17:14
@EchoTacticDouble In the demo? I don't see it missing this ..
– Alex G
Nov 21 '18 at 17:16
sorry, depends on the formatting, however it still picks up individual integers that aren't time. If it's a time without a am/pm bit, it will always have a colon
– EchoTacticDouble
Nov 21 '18 at 17:19
@EchoTacticDouble You are correct. Check updated answer.
– Alex G
Nov 21 '18 at 17:24
this is perfect, thank you!
– EchoTacticDouble
Nov 21 '18 at 17:42
add a comment |
You can use this regex:
d{1,2}:d{2}(s*[ap].?m.?)?
Explanation:
d{1,2}
will match 1 or 2 digits (for hour).
:
will match literal:
.
d{2}
will match exact 2 digits (for minutes).
(s*[ap].?m.?)?
: will either take a match fors*[ap].?m.?
or it won't, because of?
after parenthesis. Now, let's breaks*[ap].?m.?
down a little:
s*
will match zero or more white-spaces.s
for white-spaces, and*
for zero or more.
[ap]
will expect eithera
orp
.
.?
will match literal.
and?
makes it optional. Note that.
in a regex matches anything, and to match the actual dot.
, you have to do.
m
will expect am
.?
again will match an optional.
And to allow case-insensitivity, you can compile this regex with IGNORECASE
flag:
import re
r = re.compile(r'd{1,2}:d{2}(s*[ap].?m.?)?', re.IGNORECASE)
missing formats such as 1pm or 1PM and doesn't allow a space after the number such as 1 pm or 1PM but this is the closest, I'll see what I can do with this one
– EchoTacticDouble
Nov 21 '18 at 17:15
add a comment |
I know you specifically asked for regex but I would be remiss if I didn't point out dateutil parser was written with sort of thing in mind. (depending on your end goal of course)
It does a REALLY good job of taking oddball date/time/datetime strings and converting them to datetime objects.
from dateutil.parser import parse
times = [ "1:00PM", "1:00pm", "1pm", "1PM", "1:00 PM", "1:00 AM", "1:00pm", "2:00"]
for t in times:
x = parse(t)
print(f"{x} <--> {t}")
output:
2018-11-21 13:00:00 <--> 1:00PM
2018-11-21 13:00:00 <--> 1:00pm
2018-11-21 13:00:00 <--> 1pm
2018-11-21 13:00:00 <--> 1PM
2018-11-21 13:00:00 <--> 1:00 PM
2018-11-21 01:00:00 <--> 1:00 AM
2018-11-21 13:00:00 <--> 1:00pm
2018-11-21 02:00:00 <--> 2:00
Would have used this but ideally I want to do it all using regular expressions, thank you though!
– EchoTacticDouble
Nov 21 '18 at 17:20
Understood. I don't what you were doing with the values so this may not be a great option. I've used this in cases where the datetime format is not always known or irregular.
– Marcel Wilson
Nov 21 '18 at 17:35
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416028%2fwriting-regex-to-capture-many-different-date-formats%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can try this regex in python: /((0d|1d|2[0-3]|d)(((:[0-5]d))|(s*[AaPp].?[mM].?))+)/
The first part matches the first part of the hour (HH or H), then next matches the minutes if any (:MM) and the last matches am, pm and their variances.
Demo: https://regex101.com/r/oPb7xb/2
This misses times such as 5:37, more concise than my solution though
– EchoTacticDouble
Nov 21 '18 at 17:14
@EchoTacticDouble In the demo? I don't see it missing this ..
– Alex G
Nov 21 '18 at 17:16
sorry, depends on the formatting, however it still picks up individual integers that aren't time. If it's a time without a am/pm bit, it will always have a colon
– EchoTacticDouble
Nov 21 '18 at 17:19
@EchoTacticDouble You are correct. Check updated answer.
– Alex G
Nov 21 '18 at 17:24
this is perfect, thank you!
– EchoTacticDouble
Nov 21 '18 at 17:42
add a comment |
You can try this regex in python: /((0d|1d|2[0-3]|d)(((:[0-5]d))|(s*[AaPp].?[mM].?))+)/
The first part matches the first part of the hour (HH or H), then next matches the minutes if any (:MM) and the last matches am, pm and their variances.
Demo: https://regex101.com/r/oPb7xb/2
This misses times such as 5:37, more concise than my solution though
– EchoTacticDouble
Nov 21 '18 at 17:14
@EchoTacticDouble In the demo? I don't see it missing this ..
– Alex G
Nov 21 '18 at 17:16
sorry, depends on the formatting, however it still picks up individual integers that aren't time. If it's a time without a am/pm bit, it will always have a colon
– EchoTacticDouble
Nov 21 '18 at 17:19
@EchoTacticDouble You are correct. Check updated answer.
– Alex G
Nov 21 '18 at 17:24
this is perfect, thank you!
– EchoTacticDouble
Nov 21 '18 at 17:42
add a comment |
You can try this regex in python: /((0d|1d|2[0-3]|d)(((:[0-5]d))|(s*[AaPp].?[mM].?))+)/
The first part matches the first part of the hour (HH or H), then next matches the minutes if any (:MM) and the last matches am, pm and their variances.
Demo: https://regex101.com/r/oPb7xb/2
You can try this regex in python: /((0d|1d|2[0-3]|d)(((:[0-5]d))|(s*[AaPp].?[mM].?))+)/
The first part matches the first part of the hour (HH or H), then next matches the minutes if any (:MM) and the last matches am, pm and their variances.
Demo: https://regex101.com/r/oPb7xb/2
edited Nov 21 '18 at 17:23
answered Nov 21 '18 at 16:59
Alex GAlex G
1,221139
1,221139
This misses times such as 5:37, more concise than my solution though
– EchoTacticDouble
Nov 21 '18 at 17:14
@EchoTacticDouble In the demo? I don't see it missing this ..
– Alex G
Nov 21 '18 at 17:16
sorry, depends on the formatting, however it still picks up individual integers that aren't time. If it's a time without a am/pm bit, it will always have a colon
– EchoTacticDouble
Nov 21 '18 at 17:19
@EchoTacticDouble You are correct. Check updated answer.
– Alex G
Nov 21 '18 at 17:24
this is perfect, thank you!
– EchoTacticDouble
Nov 21 '18 at 17:42
add a comment |
This misses times such as 5:37, more concise than my solution though
– EchoTacticDouble
Nov 21 '18 at 17:14
@EchoTacticDouble In the demo? I don't see it missing this ..
– Alex G
Nov 21 '18 at 17:16
sorry, depends on the formatting, however it still picks up individual integers that aren't time. If it's a time without a am/pm bit, it will always have a colon
– EchoTacticDouble
Nov 21 '18 at 17:19
@EchoTacticDouble You are correct. Check updated answer.
– Alex G
Nov 21 '18 at 17:24
this is perfect, thank you!
– EchoTacticDouble
Nov 21 '18 at 17:42
This misses times such as 5:37, more concise than my solution though
– EchoTacticDouble
Nov 21 '18 at 17:14
This misses times such as 5:37, more concise than my solution though
– EchoTacticDouble
Nov 21 '18 at 17:14
@EchoTacticDouble In the demo? I don't see it missing this ..
– Alex G
Nov 21 '18 at 17:16
@EchoTacticDouble In the demo? I don't see it missing this ..
– Alex G
Nov 21 '18 at 17:16
sorry, depends on the formatting, however it still picks up individual integers that aren't time. If it's a time without a am/pm bit, it will always have a colon
– EchoTacticDouble
Nov 21 '18 at 17:19
sorry, depends on the formatting, however it still picks up individual integers that aren't time. If it's a time without a am/pm bit, it will always have a colon
– EchoTacticDouble
Nov 21 '18 at 17:19
@EchoTacticDouble You are correct. Check updated answer.
– Alex G
Nov 21 '18 at 17:24
@EchoTacticDouble You are correct. Check updated answer.
– Alex G
Nov 21 '18 at 17:24
this is perfect, thank you!
– EchoTacticDouble
Nov 21 '18 at 17:42
this is perfect, thank you!
– EchoTacticDouble
Nov 21 '18 at 17:42
add a comment |
You can use this regex:
d{1,2}:d{2}(s*[ap].?m.?)?
Explanation:
d{1,2}
will match 1 or 2 digits (for hour).
:
will match literal:
.
d{2}
will match exact 2 digits (for minutes).
(s*[ap].?m.?)?
: will either take a match fors*[ap].?m.?
or it won't, because of?
after parenthesis. Now, let's breaks*[ap].?m.?
down a little:
s*
will match zero or more white-spaces.s
for white-spaces, and*
for zero or more.
[ap]
will expect eithera
orp
.
.?
will match literal.
and?
makes it optional. Note that.
in a regex matches anything, and to match the actual dot.
, you have to do.
m
will expect am
.?
again will match an optional.
And to allow case-insensitivity, you can compile this regex with IGNORECASE
flag:
import re
r = re.compile(r'd{1,2}:d{2}(s*[ap].?m.?)?', re.IGNORECASE)
missing formats such as 1pm or 1PM and doesn't allow a space after the number such as 1 pm or 1PM but this is the closest, I'll see what I can do with this one
– EchoTacticDouble
Nov 21 '18 at 17:15
add a comment |
You can use this regex:
d{1,2}:d{2}(s*[ap].?m.?)?
Explanation:
d{1,2}
will match 1 or 2 digits (for hour).
:
will match literal:
.
d{2}
will match exact 2 digits (for minutes).
(s*[ap].?m.?)?
: will either take a match fors*[ap].?m.?
or it won't, because of?
after parenthesis. Now, let's breaks*[ap].?m.?
down a little:
s*
will match zero or more white-spaces.s
for white-spaces, and*
for zero or more.
[ap]
will expect eithera
orp
.
.?
will match literal.
and?
makes it optional. Note that.
in a regex matches anything, and to match the actual dot.
, you have to do.
m
will expect am
.?
again will match an optional.
And to allow case-insensitivity, you can compile this regex with IGNORECASE
flag:
import re
r = re.compile(r'd{1,2}:d{2}(s*[ap].?m.?)?', re.IGNORECASE)
missing formats such as 1pm or 1PM and doesn't allow a space after the number such as 1 pm or 1PM but this is the closest, I'll see what I can do with this one
– EchoTacticDouble
Nov 21 '18 at 17:15
add a comment |
You can use this regex:
d{1,2}:d{2}(s*[ap].?m.?)?
Explanation:
d{1,2}
will match 1 or 2 digits (for hour).
:
will match literal:
.
d{2}
will match exact 2 digits (for minutes).
(s*[ap].?m.?)?
: will either take a match fors*[ap].?m.?
or it won't, because of?
after parenthesis. Now, let's breaks*[ap].?m.?
down a little:
s*
will match zero or more white-spaces.s
for white-spaces, and*
for zero or more.
[ap]
will expect eithera
orp
.
.?
will match literal.
and?
makes it optional. Note that.
in a regex matches anything, and to match the actual dot.
, you have to do.
m
will expect am
.?
again will match an optional.
And to allow case-insensitivity, you can compile this regex with IGNORECASE
flag:
import re
r = re.compile(r'd{1,2}:d{2}(s*[ap].?m.?)?', re.IGNORECASE)
You can use this regex:
d{1,2}:d{2}(s*[ap].?m.?)?
Explanation:
d{1,2}
will match 1 or 2 digits (for hour).
:
will match literal:
.
d{2}
will match exact 2 digits (for minutes).
(s*[ap].?m.?)?
: will either take a match fors*[ap].?m.?
or it won't, because of?
after parenthesis. Now, let's breaks*[ap].?m.?
down a little:
s*
will match zero or more white-spaces.s
for white-spaces, and*
for zero or more.
[ap]
will expect eithera
orp
.
.?
will match literal.
and?
makes it optional. Note that.
in a regex matches anything, and to match the actual dot.
, you have to do.
m
will expect am
.?
again will match an optional.
And to allow case-insensitivity, you can compile this regex with IGNORECASE
flag:
import re
r = re.compile(r'd{1,2}:d{2}(s*[ap].?m.?)?', re.IGNORECASE)
answered Nov 21 '18 at 16:38
Muhammad AhmadMuhammad Ahmad
2,1321422
2,1321422
missing formats such as 1pm or 1PM and doesn't allow a space after the number such as 1 pm or 1PM but this is the closest, I'll see what I can do with this one
– EchoTacticDouble
Nov 21 '18 at 17:15
add a comment |
missing formats such as 1pm or 1PM and doesn't allow a space after the number such as 1 pm or 1PM but this is the closest, I'll see what I can do with this one
– EchoTacticDouble
Nov 21 '18 at 17:15
missing formats such as 1pm or 1PM and doesn't allow a space after the number such as 1 pm or 1PM but this is the closest, I'll see what I can do with this one
– EchoTacticDouble
Nov 21 '18 at 17:15
missing formats such as 1pm or 1PM and doesn't allow a space after the number such as 1 pm or 1PM but this is the closest, I'll see what I can do with this one
– EchoTacticDouble
Nov 21 '18 at 17:15
add a comment |
I know you specifically asked for regex but I would be remiss if I didn't point out dateutil parser was written with sort of thing in mind. (depending on your end goal of course)
It does a REALLY good job of taking oddball date/time/datetime strings and converting them to datetime objects.
from dateutil.parser import parse
times = [ "1:00PM", "1:00pm", "1pm", "1PM", "1:00 PM", "1:00 AM", "1:00pm", "2:00"]
for t in times:
x = parse(t)
print(f"{x} <--> {t}")
output:
2018-11-21 13:00:00 <--> 1:00PM
2018-11-21 13:00:00 <--> 1:00pm
2018-11-21 13:00:00 <--> 1pm
2018-11-21 13:00:00 <--> 1PM
2018-11-21 13:00:00 <--> 1:00 PM
2018-11-21 01:00:00 <--> 1:00 AM
2018-11-21 13:00:00 <--> 1:00pm
2018-11-21 02:00:00 <--> 2:00
Would have used this but ideally I want to do it all using regular expressions, thank you though!
– EchoTacticDouble
Nov 21 '18 at 17:20
Understood. I don't what you were doing with the values so this may not be a great option. I've used this in cases where the datetime format is not always known or irregular.
– Marcel Wilson
Nov 21 '18 at 17:35
add a comment |
I know you specifically asked for regex but I would be remiss if I didn't point out dateutil parser was written with sort of thing in mind. (depending on your end goal of course)
It does a REALLY good job of taking oddball date/time/datetime strings and converting them to datetime objects.
from dateutil.parser import parse
times = [ "1:00PM", "1:00pm", "1pm", "1PM", "1:00 PM", "1:00 AM", "1:00pm", "2:00"]
for t in times:
x = parse(t)
print(f"{x} <--> {t}")
output:
2018-11-21 13:00:00 <--> 1:00PM
2018-11-21 13:00:00 <--> 1:00pm
2018-11-21 13:00:00 <--> 1pm
2018-11-21 13:00:00 <--> 1PM
2018-11-21 13:00:00 <--> 1:00 PM
2018-11-21 01:00:00 <--> 1:00 AM
2018-11-21 13:00:00 <--> 1:00pm
2018-11-21 02:00:00 <--> 2:00
Would have used this but ideally I want to do it all using regular expressions, thank you though!
– EchoTacticDouble
Nov 21 '18 at 17:20
Understood. I don't what you were doing with the values so this may not be a great option. I've used this in cases where the datetime format is not always known or irregular.
– Marcel Wilson
Nov 21 '18 at 17:35
add a comment |
I know you specifically asked for regex but I would be remiss if I didn't point out dateutil parser was written with sort of thing in mind. (depending on your end goal of course)
It does a REALLY good job of taking oddball date/time/datetime strings and converting them to datetime objects.
from dateutil.parser import parse
times = [ "1:00PM", "1:00pm", "1pm", "1PM", "1:00 PM", "1:00 AM", "1:00pm", "2:00"]
for t in times:
x = parse(t)
print(f"{x} <--> {t}")
output:
2018-11-21 13:00:00 <--> 1:00PM
2018-11-21 13:00:00 <--> 1:00pm
2018-11-21 13:00:00 <--> 1pm
2018-11-21 13:00:00 <--> 1PM
2018-11-21 13:00:00 <--> 1:00 PM
2018-11-21 01:00:00 <--> 1:00 AM
2018-11-21 13:00:00 <--> 1:00pm
2018-11-21 02:00:00 <--> 2:00
I know you specifically asked for regex but I would be remiss if I didn't point out dateutil parser was written with sort of thing in mind. (depending on your end goal of course)
It does a REALLY good job of taking oddball date/time/datetime strings and converting them to datetime objects.
from dateutil.parser import parse
times = [ "1:00PM", "1:00pm", "1pm", "1PM", "1:00 PM", "1:00 AM", "1:00pm", "2:00"]
for t in times:
x = parse(t)
print(f"{x} <--> {t}")
output:
2018-11-21 13:00:00 <--> 1:00PM
2018-11-21 13:00:00 <--> 1:00pm
2018-11-21 13:00:00 <--> 1pm
2018-11-21 13:00:00 <--> 1PM
2018-11-21 13:00:00 <--> 1:00 PM
2018-11-21 01:00:00 <--> 1:00 AM
2018-11-21 13:00:00 <--> 1:00pm
2018-11-21 02:00:00 <--> 2:00
edited Nov 21 '18 at 17:33
answered Nov 21 '18 at 17:15


Marcel WilsonMarcel Wilson
1,5161131
1,5161131
Would have used this but ideally I want to do it all using regular expressions, thank you though!
– EchoTacticDouble
Nov 21 '18 at 17:20
Understood. I don't what you were doing with the values so this may not be a great option. I've used this in cases where the datetime format is not always known or irregular.
– Marcel Wilson
Nov 21 '18 at 17:35
add a comment |
Would have used this but ideally I want to do it all using regular expressions, thank you though!
– EchoTacticDouble
Nov 21 '18 at 17:20
Understood. I don't what you were doing with the values so this may not be a great option. I've used this in cases where the datetime format is not always known or irregular.
– Marcel Wilson
Nov 21 '18 at 17:35
Would have used this but ideally I want to do it all using regular expressions, thank you though!
– EchoTacticDouble
Nov 21 '18 at 17:20
Would have used this but ideally I want to do it all using regular expressions, thank you though!
– EchoTacticDouble
Nov 21 '18 at 17:20
Understood. I don't what you were doing with the values so this may not be a great option. I've used this in cases where the datetime format is not always known or irregular.
– Marcel Wilson
Nov 21 '18 at 17:35
Understood. I don't what you were doing with the values so this may not be a great option. I've used this in cases where the datetime format is not always known or irregular.
– Marcel Wilson
Nov 21 '18 at 17:35
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416028%2fwriting-regex-to-capture-many-different-date-formats%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Welcome to StackOverflow. Your expression can be greatly simplified using regex flags. What programming language are you using?
– Dinei
Nov 21 '18 at 16:07
@Dinei it's in Python, I'll have a look at flags
– EchoTacticDouble
Nov 21 '18 at 16:08
Try this regex, I think it's simpler than what you have now. /^(0d|1d|2[0-3]|d)(:[0-5]d)?( *)?([AaPp].?[mM].?)?$/
– Alex G
Nov 21 '18 at 16:14
The
.
character in your pattern (likea.m|p.m
) needs a "" character before (likea.m|p.m
)– Foo
Nov 21 '18 at 16:17
@TânNguyễn That's a very good spot, need to change that
– EchoTacticDouble
Nov 21 '18 at 16:24