Regex for last occurence of String between fix determinator - javascript

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.

Related

Changing a string from user input

Having some troubles figuring this one out as I'm not too sure what you'd even call it.
I'm trying to take a users input and swap it around, so for example:
1 input for the date: DD MM YYYY, I want them to input it as such and then change it to YYYYDDMM server side before sending it where it needs to go.
I've tried looking at regex expressions and str replace but it doesn't appear to have an option to pull the end of a users string and place it in a different location. Any insight would be appreciated of where I could find some more help on this or what direction to go in.
You can do it with regex if you capture each part of the date string in a group, and then echo the groups back in a different order, without the spaces:
const reformatDate = dateStr => dateStr.replace(/^(\d{2}) (\d{2}) (\d{4})$/, '$3$1$2');
console.log(reformatDate('05 12 2000'));
console.log(reformatDate('10 31 2010'));
console.log(reformatDate('01 01 2018'));
With Javascript's .replace, $ followed by a number in the second parameter passed to the .replace function replaces the $# with that captured group in the regular expression. For example, $3 gets replaced with whatever was matched by the third (...).
The same sort of replacement syntax works in PHP:
function reformatDate($dateStr) {
return preg_replace('/^(\d{2}) (\d{2}) (\d{4})$/', '$3$1$2', $dateStr);
}

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.

Extract numeric and text parts of a string, in varying formats

I'm trying to put together a RegEx to split a variety of possible user inputs, and while I've managed to succeed with some cases, I've not managed to cover every case that I'd like to.
Possible inputs, and expected outputs
"1 day" > [1,"day"]
"1day" > [1,"day"]
"10,000 days" > [10000,"days"]
Is it possible to split the numeric and text parts from the string without necessarily having a space, and to also remove the commas etc from the string at the same time?
This is what I've got at the moment
[a-zA-Z]+|[0-9]+
Which seems to split the numeric and text portions nicely, but is tripped up by commas. (Actually, as I write this, I'm thinking I could use the last part of the results array as the text part, and concatenate all the other parts as the numeric part?)
var test = [
'1 day',
'1day',
'10,000 days',
];
console.log(test.map(function (a) {
a = a.replace(/(\d),(\d)/g, '$1$2'); // remove the commas
return a.match(/^(\d+)\s*(.+)$/); // split in two parts
}));
This regular expression works, apart from removing the comma from the matched number string:
([0-9,]+]) *(.*)
You cannot "ignore" a character in a returned regular expression match string, so you will just have to remove the comma from the returned regex match afterwards.

Regex for date in format hour.minute dayName day/month

I am trying to verify str with the code below. My final goal is to allow this style of input:
18.30 Saturday_lastMatch 3/10
However, the code I have can't even work for the basic usage (98.5% str will be of this format):
19.30 Friday 15/5
var regex= /[0-9]{2}[\.:][0-9]{2} [A-Z][a-z]{4,7} [0-9]\/[0-9]{2}/;
if(!str.match(regex)) {
//"Bad format, match creation failed!");
}
What am I missing?
There are a number of problems with your regex.
The date & time matching portions at the beginning and end don't allow for 1 or 2 digit numbers as they should.
You may want to consider anchoring the regex at the beginning and end with ^ and $, respectively.
The literal dot in the character class doesn't need to be escaped.
Try this:
var regex= /^[0-9]{1,2}[.:][0-9]{1,2} [A-Z][a-z]{5,8} [0-9]{1,2}\/[0-9]{1,2}$/;
The final part of your regular expression that checks day/month needs to be expanded. It currently only matches #/##, but it should allow ##/# as well. The simplest fix would be to allow either one or two digits on either side (e.g. 12/31)
var regex= /[0-9]{2}[\.:][0-9]{2} [A-Z][a-z]{4,7} [0-9]{1,2}\/[0-9]{1,2}/;

Remove part of attribute value with jquery or javascript

There is a data parameter for a div that looks as follows:
<div data-params="[possibleText&]start=2011-11-01&end=2011-11-30[&possibleText]">
</div>
I want to remove the from the start through the end of the second date from that data-params attribute. There may or may not be text before the start and after the date after the second date.
How can I accomplish this using javascript or jQuery? I know how to get the value of the "data-params" attribute and how to set it, I'm just not sure how to remove just that part from the string.
Thank you!
Note: The dates will not always be the same.
I'd use a regular expression:
var text = $('div').attr('data-params');
var dates = text.match(/start=\d{4}-\d{2}-\d{2}&end=\d{4}-\d{2}-\d{2}/)[0]
// dates => "start=2011-11-01&end=2011-11-30"
The regular expression is not too complex. The notation \d means "match any digit" and \d{4} means "match exactly 4 digits". The rest is literal characters. So you can see how it works. Finally, that [0] at the end is because javascript match returns an array where the first element is the whole match and the rest are subgroups. We don't have any subgroups and we do want the whole match, so we just grab the first element, hence [0].
If you wanted to pull out the actual dates instead of the full query string, you can create subgroups to match by adding parenthesis around the parts you want, like this:
var dates = text.match(/start=(\d{4}-\d{2}-\d{2})&end=(\d{4}-\d{2}-\d{2})/)
// dates[0] => "start=2011-11-01&end=2011-11-30"
// dates[1] => "2011-11-01"
// dates[2] => "2011-11-30"
Here, dates[1] is the start date (the first subgroup based on parenthesis) and dates[2] is the end date (the second subgroup).
My regex skills aren't that good but this should do it
var txt = "[possibleText&]start=2011-11-01&end=2011-11-30[&possibleText]";
var requiredTxt = txt.replace(/^(.*)start=\d{4}-\d{2}-\d{2}&end=\d{4}-\d{2}-\d{2}(.*)$/, "$1$2");
I'm sure there are better ways to match your string with regex, but the $1 and $2 will put the first group and second group match into your requiredTxt stripping out the start/end stuff in the middle.
Say you have your data-params in a variable foo. Call foo.match as follows:
foo.match("[\\?&]start=([^&#]*)"); //returns ["&start=2011-11-01", "2011-11-01"]
foo.match("[\\?&]end=([^&#]*)"); //returns ["&end=2011-11-30", "2011-11-30"]

Categories