Regex combining capturing group with quantifier for SEPA bank numbers - javascript

Goal:
I am trying to create a more efficient regular expression to flexibly capture SEPA bank numbers for form validation.
Background:
SEPA IBAN numbers have this pattern: NL88ABNA1234567890
My question is concerning the last 9 or 10 numbers at the end e.g. 0123456789.
I want to allow any combination of space characters and dashes as the numbers are sanitised before submission, e.g.:-
0123456789
01-23-45-67-89
01 23 45 67 89
01 - 23 - 45 - 67 - 89
012 345 67 89
etc etc
I can achieve this with the following:-
(\d(?:\s[\s-]\s|[\s-])?\d(?:\s[\s-]\s|[\s-])?\d(?:\s[\s-]\s|[\s-])?\d(?:\s[\s-]\s|[\s-])?\d(?:\s[\s-]\s|[\s-])?\d(?:\s[\s-]\s|[\s-])?\d(?:\s[\s-]\s|[\s-])?\d(?:\s[\s-]\s|[\s-])?\d(?:\s[\s-]\s|[\s-])?\d?)
But it's not very elegant
I tried combining capturing group with a quantifier but it only returns the last digit:-
(\d(?:\s?[\s-]\s?)?){9,10}
How can I achieve this more efficiently & check the whole number not just last digit?

You can use this regex with optional spaces and hyphen following your digits:
/((?:\d[\s-]*){9,10})/
[\s-]* will allow 0 or more spaces and hyphens after each digit.
RegEx Demo

Related

regular expression to match Number + string

I am trying to match these use cases in Javascript:
a) 8 Months
b) 18 Years
Numbers could range from 1 to 20 and the strings could only be either "months" or "years"
I am trying this
[0-9]\s(Years|Months)
But it is not working.
You can use this:
([1-9]|1[0-9]|20)\s(Years|Months)
where:
[1-9] matches a digit from 1 to 9
1[0-9] matches a number from 10 to 19
20 matches just 20
Edit: As noticed in a comment, you should use
^([1-9]|1[0-9]|20)\s(Years|Months)$
if the whole string must exactly match with this text.
Another option is prepending the regex with a word boundary (\b) in order to prevent matching cases like "42 Years".

Regex to check pattern with numbers

I want to create a regex that does the following
<\numberMAX8>[space or not]<\symbol(-)>[space or not]<\numberMAX8> and max 10 times of all of this - I don't care about end spaces, also numbers must be between 5-8.
To explain it a bit more I'll give a few examples
ex:
5-6 7-6 8-8 6-7 ok
4-7 not ok //because of 4
7 - 6 ok
7-6-6-6 not ok because of the - in the middle
Below is what I have so far without having included the mid spaces.
^([5-8](?:-|\s)[5-8][\s]){1,10}
-> <-//didnt work.
Here you go:
^([5-8]\s*-\s*[5-8]\s*){1,10}$
So the explanation is:
The regex matches a starting number from 5-8 ^[5-8], then an arbitrary number of spaces \s*, then dash -, then arbitrary number of spaces \s*, then a number from 5 to 8 [5-8], then an arbitrary number of spaces \s*, and that pattern from 1 to 10 times {1,10}, and nothing after the pattern $.

How to fetch phone numbers from string using regular expression(Regex)?

I want regex which finds out continues max 12 digits long number by ignoring space, plus (+), parenthesis & dash, e.g:
Primary contact number +91 98333332343 call me on this
My number is +91-983 333 32343
2nd number +1 (983) 333 32343, call me
Another one 983-333-32343
One more +91(983)-333-32343 that's all
121 street pin code 421 728 & number is 9833636363
Currently, I have a regex, which does the job of fetching contact numbers from string:
/* This only work for the first case not for any other
and for last one it outputs "121" */
\\+?\\(?\\d*\\)? ?\\(?\\d+\\)?\\d*([\\s./-]?\\d{2,})+
So what can be done here to support all the above cases, in short ignoring special characters and length should range from 10-12.
I see that there are numbers ranging from 10 to 13 digits.
You may use
/(?:[-+() ]*\d){10,13}/g
See the regex demo.
Details:
(?:[-+() ]*\d){10,13} - match 10 to 13 sequences of:
[-+() ]* - zero or more characters that are either -, +, (, ), or a space
\d - a digit
var re = /(?:[-+() ]*\d){10,13}/gm;
var str = 'Primary contact number +91 98333332343 call me on this\nMy number is +91-983 333 32343\n2nd number +1 (983) 333 32343, call me\nAnother one 983-333-32343\nOne more +91(983)-333-32343 that\'s all\n121 street pin code 421 728 & number is 9833636363';
var res = str.match(re).map(function(s){return s.trim();});
console.log(res);
The accepted answer will match your criteria but I'd like to propose a more restrictive approach. It is quite specific to the number formats you provided :
test specifically if a string IS a number /^(\+(\d{1,2})[- ]?)?(\(\d{3}\)|\d{3})[- ]?\d{3}[- ]?\d{4,5}$/
test whether a string contains at least one number : /(\+(\d{1,2})[- ]?)?(\(\d{3}\)|\d{3})[- ]?\d{3}[- ]?\d{4,5}/
I made you a small fiddle where you can try out different regexes on any number of... well numbers : https://jsfiddle.net/u51xrcox/5/.
have fun.

Indian pincode validation regex - Only six digits, shouldn't start with `0`

I have tried Regex accept numeric only. First character can't be 0 and What is the regex for "Any positive integer, excluding 0" however that didn't work as per my requirements.
I want exact 6 digit numeric number but shouldn't start with 0
I've tried
^[1-9][0-9]{6}*$
^([^0][0-9]){6}$
...
Need fine tuning.
The problem with ^[1-9][0-9]{6}*$ is it is an invalid regex because of {6}* and ^([^0][0-9]){6}$ is that it is allowing any character that is not 0 followed by six digits.
Use
^[1-9][0-9]{5}$
Explanation:
^: Starts with anchor
[1-9]: Matches exactly one digit from 1 to 9
[0-9]{5}: Matches exactly five digits in the inclusive range 0-9
$: Ends with anchor
Regex101 Playground
HTML5 Demo:
input:invalid {
color: red;
}
<input type="text" pattern="[1-9][0-9]{5}" />
This regular expression covers;
PIN code doesn't start from zero
Allows 6 digits only
Allows 6 consecutive digits (e.g. 431602)
Allows 1 space after 3 digits (e.g. 431 602)
([1-9]{1}[0-9]{5}|[1-9]{1}[0-9]{3}\\s[0-9]{3})
Some websites and banks have the habit of spacing pincode after 3 digits.
To match both 515411 and 515 411 the following pattern will help.
^[1-9]{1}[0-9]{2}\s{0,1}[0-9]{3}$
^[1-9]{1} - PIN Code that starts with digits 1-9
[0-9]{2} - Next two digits may range from 0-9
\s{0,1} - Space that can occur once or never
[0-9]{3}$ - Last 3 needs to be digits ranging from 0-9

Regex to allow 0 to 5 hyphens within a 10 - 15 numeric character string?

I'm trying to create a regex for the following requirements:
10 numerical characters minimum
15 numerical characters maximum
Allow 0 to 5 hyphens, anywhere within string
I have this following regex, but it exceeds the 15 numerical character requirement if there aren't any hyphens:
/^([0-9]{10,15}|(?=[-]*)[0-9-]{11,19})$/
Thanks.
This regex will check for 0 to 5 hyphens and 10 to 15 digits. Any other characters are allowed.
^(?=[^-]*(-[^-]*){0,5}$)(?=\D*(\d\D*){10,15}$).*$
And this only allows digits and hyphens:
^(?=\d*(-\d*){0,5}$)(?=-*(\d-*){10,15}$).*$
Test it.
I'm pretty sure this can't be accomplished purely with one regex.
Here's an example using Python 2.6 without regular expressions (assumes input is only digits and hyphens):
input = "123-45678----9"
digitCount = len(input.translate(None, '-'))
hyphenCount = len(input.translate(None, '0123456789'))
if digitCount >= 0 and digitCount <= 15 and hyphenCount >= 0 and hyphenCount <= 5:
print "Yay!"

Categories