Regex only allow x integers? - javascript

I have some dates that look like this:
20160517124945-0600
20160322134822.410-0500
20160322134822-0500
I used RegexMagic to find this regex:
(?:[0-9]+){14}\.?(?:[0-9]+){0,3}-(?:[0-9]+){4}
The problem is it also accepts things like this:
20160322134822-05800
or
20160323542352352352134822-0500
Apparently {} doesn't mean what I thought it did. How can I ensure I can only enter 14 digits before the - (or optional .) and 4 after?

Note that your regex did not function as expected because (?:[0-9]+){0,3} means match 1 or more digits zero, one, two or three times. That means, it matched any amount of digits.
It seems you need an optional group for the . followed with 1+ digits and you need to replace + with the limiting quantifiers:
^\d{14}(?:\.\d+)?-\d{4}$
See the regex demo.
Explanation:
^ - start of string (we need to ensure we only match 14 first digits before . or -)
\d{14} - 14 digits
(?:\.\d+)? - 1 or 0 sequences of . + 1 or more digits
- - a hyphen
\d{4} - 4 digits
$ - end of string
var re = /^\d{14}(?:\.\d+)?-\d{4}$/;
var strs = ['20160517124945-0600', '20160322134822.410-0500', '20160322134822-0500', '20160322134822-05800','20160323542352352352134822-0500'];
for (var s of strs) {
document.body.innerHTML += s + ": " + re.test(s) + "<br/>";
}

Try:
^(\d{14})(\.\d{0,3})?-(\d{4})$

Here's my version:
^[0-9]{14}(\.[0-9]{1,3})?-[0-9]{4}$
For some reason I always like using [0-9] instead of \d
So we're doing the start of the string with: ^
Then 14 numbers 0-9
Then an optional group starting with a period and then up to three numbers (I'm assuming a period and then no numbers after wouldn't be acceptable. This is in contrast to what you posted)
A hyphen
Four numbers
Then the end of the string with: $

I suppose you could simplify your regexp like this:
^(\d{14})(\.\d{3})?(-\d{4})$
test https://regex101.com/r/lF9gX0/1

Related

Using Regular Expression to validate the following requirement?

i am trying to validate a number exactly 15 digits with 5 digits group separated by "-" , ":" , "/", that should start with either 5 or 4 or 37, it also should not contain any characters.
It means
37344-37747-27744 - valid
40344-37747-27744 - valid
59344-37747-27744 - valid
37344:37747:27744 - valid
37344/37747/27744 - valid
87887-98734-83422 - Not valid
548aa:aa764:90887 - not valid
37759\29938\92093 - not valid
I was only able to go this far
\d{5}[-:\/]\d{5}[-:\/]\d{5}
Please help.
Try this one:
(((5|4)\d{4})|((37)\d{3}))[-:\/]\d{5}[-:\/]\d{5}
https://regex101.com/r/VvTq5P/1
Edit:
I would also add: \b at the beggining and at the end:
\b(((5|4)\d{4})|((37)\d{3}))[-:\/]\d{5}[-:\/]\d{5}\b
so nothing like:
12337344-37747-27744
can pass the test.
You could use look ahead (?=.) to first check if your string starts ^ with numbers 5|4|37 that is the requirement, here's full pattern:
^(?=5|4|37)\d{5}[-:\/]\d{5}[-:\/]\d{5}$
Demo here
If I understand correctly the regex is this:
((5|4)\d{4}|37\d{3})[-:\/]\d{5}[-:\/]\d{5}
Needs to be
4XXXX, 5XXXX or 37XXX
So I split it up to accept the 3 forms
((5|4)\d{4}|37\d{3})[-:\/]\d{5}[-:\/]\d{5}
(5|4)\d{4} - looks for a number that starts with either 5 or 4 and four digits afterward.
Then the or 37\d{3} looks for 37 and three digits afterward.
If the separators have to be the same:
^(?=37|[54])\d{5}([-:\/])\d{5}\1\d{5}$
Explanation
^ Start of string
(?=37|[54]) Positive lookahead, assert either 37 or 5 or 4 to the right
\d{5} Match 5 digits
([-:\/]) Capture group 1, match one of - : /
\d{5} Match 5 digits
\1 Backreference to match the same separator as in group 1
\d{5} Match 5 digits
$ End of string
See a regex101 demo.
const regex = /^(?=37|[54])\d{5}([-:\/])\d{5}\1\d{5}$/;
[
"37344-37747-27744",
"40344-37747-27744",
"59344-37747-27744",
"37344:37747:27744",
"37344/37747/27744",
"87887-98734-83422",
"548aa:aa764:90887",
"37759\\29938\\92093",
"37344-37747/27744",
"37344:37747-27744"
].forEach(s =>
console.log(`${s} --> ${regex.test(s)}`)
)
The pattern without a lookaround using an alternation:
^(?:37\d{3}|[54]\d{4})([-:\/])\d{5}\1\d{5}$
See a regex101 demo
const regex = /^(?:37\d{3}|[54]\d{4})([-:\/])\d{5}\1\d{5}$/;
[
"37344-37747-27744",
"40344-37747-27744",
"59344-37747-27744",
"37344:37747:27744",
"37344/37747/27744",
"87887-98734-83422",
"548aa:aa764:90887",
"37759\\29938\\92093",
"37344-37747/27744",
"37344:37747-27744"
].forEach(s =>
console.log(`${s} --> ${regex.test(s)}`)
)

Regexp to allow letters, numbers and -

I have a problem with creating regexp to allow:
exactly 2 letters at the beginning
exactly 10 numbers after letters
at most 4 "-" between numbers (should allow also without it)
so example of valid string is GB123-55-22-22-6,
my current regexp is: /^([A-Z]{2})?[0-9]{10}$/. He allow GB1235522226 but I have a problem with "-".
can somoene tell me how to allow this regexp to use at most 4 "-" chars?
thanks for any help!
You can use
^(?:[A-Z]{2})?(?=(?:-?\d){10}$)[0-9]+(?:-[0-9]+){0,4}$
See the regex demo.
Details:
^ - start of string
(?:[A-Z]{2})? - optional two letters
(?=(?:-?\d){10}$) - there must be 10 digits optionally separated with a - till end of string, the string must end with a digit
[0-9]+ - one or more digits
(?:-[0-9]+){0,4} - zero to four occurrences of a hyphen and then one or more digits
$ - end of string.

RegEx for validating three alphanumeric conditions

I have the current regex that works fine:
^[a-zA-Z]{2}[0-9]{8}$
It works for existing pattern containing exactly 2 letters followed by exacly 8 digits, such as AB12345678 or yZ01928374
I need to change it to a new regex that satisfies 3 conditions:
1) if the first 2 letters are followed by a single digit "9", then it can be followed by 7 or 8 digits [0-9]{7,8}. Eg AB91234567 or AC912345678 are both valid.
2) if the first 2 letters are followed by a single digit between 0-5, then it can be followed by exactly 8 digits [0-9]{8}. Eg AB412345678
3) if the first 2 letters are followed by digit between 6-8, then it can be followed by exactly 7 digits [0-9]{7}. Eg AB71234567
I've got bits and pieces but I'm really not sure how to put all 3 conditions together in 1 nice expression.
You can use alternation and add the desired rules for values followed by after first two alphabets
let test = ['AB91234567','AB412345678','AB912','ABC123']
test.forEach( value => {
console.log(/^[a-zA-Z]{2}(?:9\d{7,8}|[0-5]\d{8}|[6-8]\d{7})$/.test(value))
})
You can use alternations to define different conditions and use this regex,
^[a-zA-Z]{2}(?:9[0-9]{7,8}|[0-5][0-9]{8}|[6-8][0-9]{7})$
Explanation:
^ - Start of string
[a-zA-Z]{2} - Matches two alphabets
(?: - Start of non-grouping pattern
9[0-9]{7,8} - If the next digit is 9 then it can be followed by 7 to 8 digits
| - Alternation
[0-5][0-9]{8} - If the next digit is zero to five then it can only have eight digits
| - Alternation
[6-8][0-9]{7}) - If the next digit is six to eight then it can only have seven digits and end of non-group pattern
$ - End of string
Regex Demo
JS Code demo,
const arr = ['ab91234567','ab912345678','ab9123456789','sd012345678','sd0123456789','df71234567','df712345678']
arr.forEach(x => console.log(x + " --> " + /^[a-zA-Z]{2}(?:9[0-9]{7,8}|[0-5][0-9]{8}|[6-8][0-9]{7})$/.test(x)))

Conditional regex replace in nodejs

I have an address like this:
117042,ABC DEF,HIJ KLMNOP,9,170
and want to have
117042,ABC DEF,HIJ KLMNOP 9 170
I tried it with this replace Regex
address = address.replace(/,[\d]/g, " ");
but this results in
117042,ABC DEF,HIJ KLMNOP 70
I do not want to replace the digit but still need to check if the digit comes after the comma to not match the other commas.
I am not very good with regex thats why I am asking for help.
You may only replace commas after numbers if they occur at the end of string:
var s = "117042,ABC DEF,HIJ KLMNOP,9,170";
var res = s.replace(/,(\d+)(?=(?:,\d+)*$)/g, " $1");
console.log(res);
The ,(\d+)(?=(?:,\d+)*$) regex matches:
, - a comma
(\d+) - (Group 1, referred to via $1 from the replacement pattern) one or more digits
(?=(?:,\d+)*$) - a positive lookahead that requires 0+ sequences of , + one or more digits at the end of the string.

How to match digit in middle of a string efficiently in javascript?

I have strings like
XXX-1234
XXXX-1234
XX - 4321
ABCDE - 4321
AB -5677
So there will be letters at the beginning. then there will be hyphen. and then 4 digits. Number of letters may vary but number of digits are same = 4
Now I need to match the first 2 positions from the digits. So I tried a long process.
temp_digit=mystring;
temp_digit=temp_digit.replace(/ /g,'');
temp_digit=temp_digit.split("-");
if(temp_digit[1].substring(0,2)=='12') {}
Now is there any process using regex / pattern matching so that I can do it in an efficient way. Something like string.match(regexp) I'm dumb in regex patterns. How can I find the first two digits from 4 digits from above strings ? Also it would be great it the solution can match digits without hyphens like XXX 1234 But this is optional.
Try a regular expression that finds at least one letter [a-zA-Z]+, followed by some space if necessary \s*, followed by a hyphen -, followed by some more space if necessary \s*. It then matches the first two digits \d{2} after the pattern.:
[a-zA-Z]+\s*-\s*(\d{2})
may vary but number of digits are same = 4
Now I need to match the first 2 positions from the digits.
Also it would be great it the solution can match digits without hyphens like XXX 1234 But this is optional.
Do you really need to check it starts with letters? How about matching ANY 4 digit number, and capturing only the first 2 digits?
Regex
/\b(\d{2})\d{2}\b/
Matches:
\b a word boundary
(\d{2}) 2 digits, captured in group 1, and assigned to match[1].
\d{2} 2 more digits (not captured).
\b a word boundary
Code
var regex = /\b(\d{2})\d{2}\b/;
var str = 'ABCDE 4321';
var result = str.match(regex)[1];
document.body.innerText += result;
If there are always 4 digits at the end, you can simply slice it:
str.trim().slice(-4,-2);
here's a jsfiddle with the example strings:
https://jsfiddle.net/mckinleymedia/6suffmmm/

Categories