I'm trying to get a regexp to get the the [HH:mm] out of the following, let's say:
Hello, today its <date[dd/mm/yy]> and the time is <time[HH:mm]>
The things between the [] can be different, it could be dd/mm or mm/yy for the date, HH:mm:ss or mm:ss for the time...
I tried to get something working with wikipedia or regular-expressions.info, but well, I failed, so I need you to help me!:)
Thanks in advance
/\d\d:\d\d$/g
See it working at http://refiddle.com/111
This basically looks for the HH:MM pattern at the end of the string.
If you need to match anywhere in the string, then this will work
/\b\d\d:\d\d\b/g
See it at http://refiddle.com/112.
Related
I was given some very long csv files and have been tasked with making the information in them readable. I want to turn time formats like 2022-08-07, 08:00:00 into a time format like 08/07/2022 8:00:00. I was looking at wild cards and was going to start with replacing the dashes with slashes first and tried -replace "(....{-}..{-}..","\1/\2/\3/" with no change to the string. I found a similar post that mentioned some regex that can helped him achieve something similar? If anyone can help me out or point me to some resources to learn more about this that would be great.
You could do what you are describing this way:
$OldDate = '2022-08-07, 08:00:00'
if ($OldDate -match '(?<Year>\d{4})-(?<Month>\d\d)-(?<Day>\d\d), (?<Hour>\d\d):(?<Minute>\d\d):(?<Seconds>\d\d)') {
Write-Host "$($Matches.Month)/$($Matches.Day)/$($Matches.Year) $($Matches.Hour):$($Matches.Minute):$($Matches.Seconds)"
}
But I think life would be a lot simpler to convert the string to a DateTime and then back to a string:
$OldDate = '2022-08-07, 08:00:00'
[DateTime]::ParseExact($OldDate, "yyyy-MM-dd, hh:mm:ss", $null).ToString('MM/dd/yyyy h:mm:ss')
I am looking for a regex to match my date format in
mm/dd/yyyy hh:mm:ss.SSS AM/PM
I found the following online
^(((0[13578]|1[02])/.-/.-\s(0[0-9]|1[0-2]):(0[0-9]|[1-59]\d):(0[0-9]|[1-59]\d)\s(AM|am|PM|pm))|((0[13456789]|1[012])/.-/.-\s(0[0-9]|1[0-2]):(0[0-9]|[1-59]\d):(0[0-9]|[1-59]\d)\s(AM|am|PM|pm))|((02)/.-/.-\s(0[0-9]|1[0-2]):(0[0-9]|[1-59]\d):(0[0-9]|[1-59]\d)\s(AM|am|PM|pm))|((02)/.-/.-\s(0[0-9]|1[0-2]):(0[0-9]|[1-59]\d):(0[0-9]|[1-59]\d)\s(AM|am|PM|pm)))$
This does not match my date
06/12/2014 12:45:56.12 AM
How to tweek the above to accept milliseconds also ?
A simple and fast solution:
\d\d\/\d\d\/\d\d\d\d \d\d\:\d\d\:\d\d\.\d\d\d (AM|PM|am|pm)
If you want to use it in PCRE you need to add a delimiter like # in start and end of pattern:
#\d\d\/\d\d\/\d\d\d\d \d\d\:\d\d\:\d\d\.\d\d\d (AM|PM|am|pm)#
For more accuracy:
([0]\d|1[012])\/([012]\d|3[01])\/\d\d\d\d ([01]\d|2[0123])\:[012345]\d\:[012345]\d\.\d\d\d (AM|PM|am|pm)
Well, first one can accept invalid values like 99/99/9999 99:99:99.999 AM I prefer to go with second one because micro-optimization is not good on 99.99% of times :)
I am trying to find the correct regex (for use with Java and JavaScript) to validate an array of day-of-week and 24-hour time formats. I figured out the time format but am struggling to come up with the full solution.
The regex needs to validate patterns which include one or more of the following, separated by a comma.
{two-character day} HH:MM-HH:MM
Three examples of valid strings would be:
M 5:30-7:00
M 5:30-7:00, T 5:30-7:00, W 18:00-19:30
F 12:00-14:30, Sa 6:45-8:15, Su 6:45-8:15
This should validate a 24-hour time:
/^((M|T|W|Th|Fr|Sa|Su) ([01]?[0-9]|2[0-3]):[0-5][0-9]-([01]?[0-9]|2[0-3]):[0-5][0-9](, )?)+$/
Credit for the time bit goes to mkyong: http://www.mkyong.com/regular-expressions/how-to-validate-time-in-24-hours-format-with-regular-expression/
you can try this
[A-Za-z]{1,2}[ ]\d+:\d+-\d+:\d+
You could try this: ([MTWFS][ouehra]?) ([0-9]|[1-2][0-9]):([0-6][0-9])-([0-9]|[1-2][0-9]):([0-6][0-9])
I'd go with this:
(((M|T(u|h)|W|F|S(a|u)) ((1*\d)|(2[0-3])):[1-5]\d-((1*\d)|(2[0-3])):[1-5]\d(, )?)+
This should do the trick:
^(M|Tu|W|Th|F|Sa|Su) \d{1,2}:\d{2}-\d{1,2}:\d{2}(, (M|Tu|W|Th|F|Sa|Su) \d{1,2}:\d{2}-\d{1,2}:\d{2})*$
Note that you show T in your example above which is ambiguous. You might want to enforce Tu and Th as shown in my regex.
This will capture all sets in an array. The T in the short day of week list is debatable (tuesday or thursday?).
^((?:[MTWFS]|Tu|Th|Sa|Su)\s(?:[0-9]{1,2}:[0-9]{2})-(?:[0-9]{1,2}:[0-9]{2})(?:,\s)?)+$
The (?:) are non-capturing groups, so your actual matches will be (for example):
M 5:30-7:00
T 5:30-7:00
W 18:00-19:30
But the entire line will validate.
Added ^ and $ for line boundaries and an explicit time-time match because some regular expression parsers may not work with the previous way that I had it.
I have the following date string - "2013-02-20T17:24:33Z"
I want to write a regex to extract just the date part "2013-02-20". How do I do that? Any help will be appreciated.
Thanks,
Murtaza
You could use capture group for this.
/(\d{4}-\d{2}-\d{1,2}).*/
Using $1, you can get your desired part.
Well straightforward approach would be \d\d\d\d-\d\d-\d\d but you can also use quantifiers to make it look nicer \d{4}-\d{2}-\d{2}.
Just search for the first T and use substring. I assume you always get a well-formatted date string.
If the date string is not guaranteed to be valid, you can use any date related library to parse and validate the input (validation includes the calendar logic, which regex fails to achieve), and reformat the output.
No sample code, since you didn't mention the language.
using substring
string date = "2013-02-20T17:24:33Z";
string h = date.Substring(0, 10);
I am trying to extract dates from a text variable.
I have created a regex which extracts 'MOST' formats of date as follows:
$regexp = '#[0-9]{2,4}[-\/ ]{1}([A-Za-z]{3}|[0-9]{2})[-\/ ]{1}[0-9]{2,4}#';
preg_match_all($regexp, $output, $dates);
It does not however extract dates of the format '08 Aug 2012' and I do not know why.. As far as I can tell.. it should..
For now I have inserted a seperate regex which works:
$regexp = '#[0-9]{2}[ ]{1}[A-Za-z]{3}[ ]{1}[0-9]{4}#';
preg_match_all($regexp, $output, $dates);
which is essentially the same..
It however seems pointless to have multiple regex when I need only have one.
If anyone could tell me why the first regex isnt working for such a format, and explain why, it would be greatly appreciated.
Thanks
Well, your regexp is correct for the date format you presented. And as such it also works without problems: http://ideone.com/XxdKV