Specific match with "OR condition" - regex - javascript

I'm making a regex to match datetime with these formats:
d/M/yyyy
d-M-yyyy
M/d/yyyy
M-d-yyyy
dd/MM/yyyy
dd-MM-yyyy
MM/dd/yyyy
MM-dd-yyyy
...
I've tried:
var reg = /^(\d{1,4}[/\-]\d{1,2}[/\-]\d{1,4}\s(\d{1,2}:){2}\d{1,2})$/;
alert(reg.test('18/07/2016 00:00:00'));
It works fine. But the problem is: it still works with this format:
d/M-yyyy
//or
dd-MM/yyyy
I saw the index of the problem is in [/\-]. I need it's duplicate correct / OR - (only one).
My question: how can I fix it?

Use a capturing group:
^\d{1,4}([/-])\d{1,2}\1\d{1,4}\s(\d{1,2}:){2}\d{1,2}$
\1 will match the same text as what matched the 1st capturing group, which is is ([/-]).

Related

Regex for last occurence of String between fix determinator

So I am having a specific string returned in the following structure:
"http://www.google.com/search","XYZ","Some other Value","false","false","2017-12-13"
I only want to find the last occurrence of a date from the format "YYYY-MM-dd", as it could also be, that the values "false" may be returning a date.
I am not experienced in regular expressions, but the last thing I achieved was to receive 2017-12-12"* with the following expression :
**((?:[^"]"*){10})$**
Looking for the occurrence of a date in the desired format won't help, as multiple dates might occur.
This is why I want to check for the last String between quotation marks. How will I get this date without quotation marks?
I want to check for the last String between quotation marks
Just:
regexp_replace(myvalue, '^.*"([^"]+)"$', '\1')
Regexp breakdown:
^ beginning of the string
.* any sequence of 0 to N characters
" double quote
( beginning of the capturing group
[^"]+ as many characters as possible other than a double quote (at least one)
) end of the capturing group
" double quote
$ end of string
The regexp matches on the entire string and replaces it with the catpured part.
Demo on DB Fiddle:
with t as (select '"http://www.google.com/search","XYZ","Some other Value","false","false","2017-12-13"' myvalue from dual)
select regexp_replace(myvalue, '^.*"([^"]+)"$', '\1') mydate from t
| MYDATE |
| :--------- |
| 2017-12-13 |
If needed, you can be more specific by specifying the expected date format in the capturing group:
regexp_replace(myvalue, '^.*"(\d{4}-\d{2}-\d{2})"$', '\1')
You could use the following regex to do it:
/^(?:".*?",)*"(.*?)"$/
Alternatively, you could use String#split() and String#slice():
const output=input
.split(',')
.pop()
.slice(1,-1)
Use simple regexp_substr as following:
select regexp_substr('"http://www.google.com/search","XYZ","Some other Value","false","false","2017-12-13"',
'((\d){4}(-)(\d){2}(-)(\d){2})"$',1,1,null,1)
from dual;
db<>fiddle demo
Here, parameters of the regexp_substr are as follows:
REGEXP_SUBSTR( string, pattern [, start_position [, nth_appearance [, match_parameter [, sub_expression ] ] ] ] )
Cheers!!
If I understand correctly, "false", "false", "2017-12-13" can be any combination of three dates or three falses, or anywhere in between, and you're looking to find the last date. I would use
(\d{4}-\d\d-\d\d(?!.*\d{4}-\d\d-\d\d))
Which will capture a date as long as it is not followed by another.
See my example here:
https://regex101.com/r/GAkpDI/1/
"http://www.google.com/search","XYZ","Some other Value","2018-04-09","false","<<2017-12-13>>"
"http://www.google.com/search","XYZ","Some other Value","<<2018-12-09>>","false","false"
"http://www.google.com/search","XYZ","Some other Value","false","<<2000-09-17>>","false"
"http://www.google.com/search","XYZ","Some other Value","2018-12-09","2000-09-17","<<2017-12-13>>"
I have put the matches inside of <<>>.
Edit: If you don't know the format the date will be in, then you could swap it for something that just finds numbers, dashes, and slashes:
https://regex101.com/r/GAkpDI/2/
([\d-\/]+(?!.*[\d-\/]+))
This will be more likely to fail, as it will find anything [0123456789-/], but if your program will only ever put a date or false in there, it should still be viable.

Regex to detect whitespace inside my words

I'm trying to match my timestamp format, but I need to detect whether it is invalid format or not (I need to do something about the invalid format)
Currently, I need to match a space character inside my timestamp:
examples:
[02:21.10,E] or [02:21.10,C#] //correct format
[02:21.10, E] or [10.21.10,E ] //incorrect format, there is a space, but i need to match it with my incorrectRegex
for my correct regex, I use this regex and it is valid:
/\[(\d\d:\d\d\.\d\d),(.*?)\]/g
and for my incorrect regex, I use
/\[\d\d:\d\d\.\d\d,\s*?\]/g
but it didn't find any match for my incorrect format
nb: I'm using a javascript
Try this:
\[\d\d[.:]\d\d[.:]\d\d,(?:[ ][^\]]+?|[^\]]+?[ ])\]
Live Demo.
var re = /\[\d\d[.:]\d\d[.:]\d\d,(?:[ ][^\]]+?|[^\]]+?[ ])\]/;
console.log(re.test('[02:21.10,E]'));;
console.log(re.test('[02:21.10,C#]'));
console.log(re.test('[02:21.10, E]'));
console.log(re.test('[10.21.10,E ]'));
console.log(re.test('[10.21.10,C# ]'));
console.log(re.test('[02:54.97,C#]single[02:55.61,A ]'));
PS: I assume it is not an error that your second invalid sample [10.21.10,E ] does not contain a : after the first two digits. I have applied the same for the second dot/colon.

regex search date in string

I want to search for a date at a string with javascript. For example:
string.search(dateReg);
Then, show the date when I find it.
I found a really nice regex at (http://regexr.com/3eoib). It works on my string: 27.11. or 27.11.2016, but it does not work on abcde 27.11.2016 fghi (result: -1).
The regex can't find the date because of these charaters in front and behind the date :/. I googled for 2 hours but didn't found an anwser (how to change the regex the right way?). I also looked at the basis regex-expressions but I coundn't find an answer :/.
Does someone know how to filter the date out of the string?
Thank you :-).
You could try the same code, but replace $ and ^ with regex word boundry \b. The code should look like this:
(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})(?=\W)|\b(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])?|(?:(?:16|[2468][048]|[3579][26])00)?)))(?=\W)|\b(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))(\4)?(?:(?:1[6-9]|[2-9]\d)?\d{2})?(?=\b)
The code above will match:
30/04/2016
31/05/2016
But it will not match:
31/04/2016
32/05/2016
and it will match any date that has a string before/after it:
abcde 27.11.2016
Demo: https://regex101.com/r/Hs2sjW/5
Update:
The previous code could have some issues. The best way to do this is to check date pattern first, then check the validity of the date. The first regex that check the date pattern could be something like this:
\d{2}[-.\/]\d{2}(?:[-.\/]\d{2}(\d{2})?)?
Then check the validity of the date with your regex. Here is a working javascript:
var myString = "Test 22/10/20 Test"; //Could be any String
var myRegexp = /\d{2}[-.\/]\d{2}(?:[-.\/]\d{2}(\d{2})?)?/g; //Check pattern only
var validDate = /(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])?|(?:(?:16|[2468][048]|[3579][26])00)?)))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))(\4)?(?:(?:1[6-9]|[2-9]\d)?\d{2})?$/g; //Check the validity of the date
myString = myRegexp.exec(myString)
myString = validDate.exec(myString[0])
console.log(myString[0])
I hope this helps you!
/(\d{1,2}[\.\/]){2,2}(\d{2,4})?/g

Remove space in a date string

I'm getting a date from a database that sometimes has spaces in the times (there isn't a way to clean up DB as I don't have control). I need to remove the spaces so I can format it to a Javascrpt date.
For example:
5 : 03 PM
As you can see, this is incorrect. I've looked at Moment.js and Regex to remove spaces around the colon, but not sure how that's done or if that's the proper way. Is there a way to remove this space?
Thanks all!
Only to delete the space around the colon, try the following; (where $1 refers to the first capture group, that is the only one (:))
a.replace(/\s*(:)\s*/, "$1");
or if you wish to simplify it further, without any capturing group;
a.replace(/\s*:\s*/, ":");
If you want to, you can actually parse that date string with Moment.js, you just need to include the spaces in your formatting string:
var time = moment('5 : 03 PM', 'h : m A');
Then you can format it however you want:
time.format('h:mmA'); // "5:30PM"
time.format('hh:mm A'); // "05:03 PM"
time.format('HH:mm'); // "17:03"
For completeness you could aviod regexp:
'5 : 03pm'.split(':')
.map(function(part) { return part.trim(); })
.join(':');

Check dateformat with regex in JS

My question is simple but takes work. I tried lots of regex expressions to check my datetime is ok or not, but though I am sure my regex exprerssion is correct it always return to me isnotok with ALERT. Can you check my code?
validateForLongDateTime('22-03-1981')
function validateForLongDateTime(date){
var regex=new RegExp("/^\d{2}[.-/]\d{2}[.-/]\d{4}$/");
var dateOk=regex.test(date);
if(dateOk){
alert('ok');
}else{
alert('notok');
}
}
There are at least 2 issues with the regex:
It has unescaped forward slashes
The hyphen in the character classes is unescaped and forms a range (matching only . and /) that is not what is necessary here.
The "fixed" regex will look like:
/^\d{2}[.\/-]\d{2}[.\/-]\d{4}$/
See demo
However, you cannot validate dates with it since it will also match 37-67-5734.
Here is an SO post with a comprehensive regex approach that looks viable
Here is my enahanced version with a character class for the delimiter:
^(?:(?:31([\/.-])(?:0?[13578]|1[02]))\1|(?:(?:29|30)([\/.-])(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29([\/.-])0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])([\/.-])(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$
Here is an SO post showing another approach using Date.parse
this way you can validate date between 1 to 31 and month 1 to 12
var regex = /^(0[1-9]|[12][0-9]|3[01])[- \/.](0[1-9]|1[012])[- \/.](19|20)\d\d$/
see this demo here https://regex101.com/r/xP1bD2/1

Categories