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.
Related
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.
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.
I'm writing an application that requires color manipulation, and I want to know when the user has entered a valid hex value. This includes both '#ffffff' and '#fff', but not the ones in between, like 4 or 5 Fs. My question is, can I write a regex that determines if a character is present a set amount of times or another exact amount of times?
What I tried was mutating the:
/#(\d|\w){3}{6}/
Regular expression to this:
/#(\d|\w){3|6}/
Obviously this didn't work. I realize I could write:
/(#(\d|\w){3})|(#(\d|\w){6})/
However I'm hoping for something that looks better.
The shortest I could come up with:
/#([\da-f]{3}){1,2}/i
I.e. # followed by one or two groups of three hexadecimal digits.
You can use this regex:
/#[a-f\d]{3}(?:[a-f\d]{3})?\b/i
This will allow #<3 hex-digits> or #<6 hex-digits> inputs. \b in the end is for word boundary.
RegEx Demo
I had to find a pattern for this myself today but I also needed to include the extra flag for transparency (i.e. #FFF5 / #FFFFFF55). Which made things a little more complicated as the valid combinations goes up a little.
In case it's of any use, here's what I came up with:
var inputs = [
"#12", // Invalid
"#123", // Valid
"#1234", // Valid
"#12345", // Invalid
"#123456", // Valid
"#1234567", // Invalid
"#12345678", // Valid
"#123456789" // Invalid
];
var regex = /(^\#(([\da-f]){3}){1,2}$)|(^\#(([\da-f]){4}){1,2}$)/i;
inputs.forEach((itm, ind, arr) => console.log(itm, (regex.test(itm) ? "valid" : "-")));
Which should return:
#123 valid
#1234 valid
#12345 -
#123456 valid
#1234567 -
#12345678 valid
#123456789 -
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}/;
Just as the title says...i'm trying to parse a string for example
2x + 3y
and i'm trying to get only the coefficients (i.e. 2 and 3)
I first tokenized it with space character as delimiter giving me "2x" "+" "3y"
then i parsed it again to this statement to get only the coefficients
var number = eqTokens[i].match(/(\-)?\d+/);
I tried printing the output but it gave me "2,"
why is it printing like this and how do i fix it? i tried using:
number = number.replace(/[,]/, "");
but this just gives me an error that number.replace is not a function
What's wrong with this?
> "2x + 3y".match(/-?\d+(?=[A-Za-z]+)/g)
[ '2', '3' ]
The above regex would match the numbers only if it's followed by one or more alphabets.
Match is going to return an array of every match. Since you put the optional negative in a parentheses, it's another capture group. That capture group has one term and it's optional, so it'll return an empty match in addition to your actual match.
Input 2x -> Your output: [2,undefined] which prints out as "2,"
Input -2x -> Your output: [2,-]
Remove the parentheses around the negative.
This is just for the sake of explaining why your case is breaking but personally I'd use Avinash's answer.