I'm trying to know how many digits there is in a string that is essentially like a password.
For now I have this regex :
^(?=.*[0-9]{3,})([a-zA-Z0-9_/+*.-]{6,})$
It works great when their is 3 digits in a row but not when they are separated in the whole string.
I need to be able to know if there is 3 digit in strings like those :
h123dasd
1hkh/23jd
1gvbn/*2fefse-
What can I do ?
You can use this regex:
/^(?=(?:\D*\d){3,})[a-zA-Z0-9_/+*.-]{6,}$/
This will enforce 3 digits in your input that may or may not be consecutive.
RegEx Demo
No need for such a complicated regex IMO - just extract digits from the string, concat the matches, and then check the length. Something like:
str.match(/\d+/g).reduce((p, c) => p + c).length > 3;
DEMO
Related
I am trying create regex witch will start with some number like 230, 420, 7456. Then could be some number in interval 1-9 but the final length must be 9.
For example 230888333 or 745623777
I create this:
([(230|420|7456)[0-9]{1,}]{9})
But it is not correct. Any idea how to make this correct?
Thaks.
The pattern that you tried is not anchored, and the current notation uses a character class [...] instead of a grouping (the ]{9} part at the end repeats 9 times a ] char)
If you use C# then use [0-9] to match a digit 0-9.
You can either assert 9 digits in total:
^(?=[0-9]{9}$)(?:230|420|7456)[0-9]+$
Regex demo
Or write an alternation for different leading lengths of digits:
^(?:(?:230|420)[0-9]{6}|7456[0-9]{5})$
Regex demo
You can simply add a check for length first and then simple regex
function checkString(str){
return str.length === 9 && /^(?:230|420|7456)\d+$/.test(str)
}
console.log(checkString("230888333"))
console.log(checkString("745623777"))
console.log(checkString("123"))
console.log(checkString("230888333123"))
I am trying to solve 'JavaScript Algorithms and Data Structures Projects: Telephone Number Validator' #freeCodeCamp.
I need to test if string contains 10 digits and what I've come up with returns false and I don't understand why.
console.log(/\d{10}/g.test("555-555-5555"))
If you want to do this with a single regular expression, you can use:
console.log(/^(?:\D*\d){10}\D*$/g.test("555-555-5555"))
console.log(/^(?:\D*\d){10}\D*$/g.test("555-555-55555"))
requiring the input to be composed of exactly 10 digits in addition to any number of other non-digit characters - but replacing non-digits with the empty string first would be a more intuitive and readable solution.
Here \d{10} means ten consecutive digits, not "ten digits with whatever in the middle, that's cool".
If want just 10 digits, you may want to strip non-digit data first:
let number = "555-555-5555";
// Remove all non-digit values (\D) which leaves only digits
let digits = number.replace(/\D/g, '').length;
It seems that your idea is correct, but this specific challenge has so many options for dashes [-] and parentheses [()] as inputs that there is a more efficient way to pass this.
function telephoneCheck(str) {
let phoneRegex = /^(1\s?)?(\d{3}|\(\d{3}\))[\s\-]?\d{3}[\s\-]?\d{4}$/
return phoneRegex.test(str);
}
The above is a way to complete the challenge in a single line of Regex, which can save you (or anyone else reading this in the future) a lot of time and space! Cheers
I want to validate a form number against an array of regular expressions.
For now, I have this:
static bool isValidPhoneNumber(String input) {
final RegExp regex = new RegExp(r'^\(\d\d\d\)\d\d\d\-\d\d\d\d\d$');
return regex.hasMatch(input);
}
The above works for a number in the format (734)637-78673.
But I want to match also for formats where the country code maybe 1 or 2 digits long
(1)498-5539867, (23)938-6738983
(\d{1,3})\d{3}-\d*
The regex above should match all your phone numbers with a country code of 1-3 digits. I don't know how long the last row of digits can be so I added "*". You can replace it with the amount you need.
I'm trying to build up a regex pattern for the html input field which only allows up to 20 combined alphabetical letters and digits which can only have up to two of Dashes(-), Underscores(_) and fullstops (.)
So something like only two of the symbols allowed and any amount of letters and digits allowed, combined they've got to be between 4 and 20.
What would the pattern for this be?
An sample (non functioning) version could be like [A-Za-z0-9([\._-]{0,2})]{4,20}
Solution:
I decided to go with #pascalhein #Honore Doktorr answer which is to use a lookahead.
The final pattern is ^(?=[A-Za-z0-9]*([._-][A-Za-z0-9]*){0,2}$)[A-Za-z0-9._-]{4,20}$
You can verify the length with a lookahead at the beginning:
^(?=.{4,20}$)
Then list all the cases that are allowed for your regex separately:
[A-Za-z0-9]* (no special chars)
[A-Za-z0-9]*[._-][A-Za-z0-9]* (one special char)
[A-Za-z0-9]*[._-][A-Za-z0-9]*[._-][A-Za-z0-9]* (two special chars)
It isn't beautiful, but I believe it should work. This is the final expression:
^(?=.{4,20}$)([A-Za-z0-9]*|[A-Za-z0-9]*[._-][A-Za-z0-9]*|[A-Za-z0-9]*[._-][A-Za-z0-9]*[._-][A-Za-z0-9]*)$
Edit:
Actually, it might be nicer to test the number of special characters with a lookahead instead:
^(?=[A-Za-z0-9]*([._-][A-Za-z0-9]*){0,2}$)[A-Za-z0-9._-]{4,20}$
I encourage you to not to use a Regex when there are functions for the porpuse you want to achieve.
An advice I give to jrs on this topic is to use a regex for single porpuses, if there is more than one porpuse, use more regex.
to answer your question:
1 your valid characters from start to end.
var 1stregex = /^[A-Za-z0-9._-]{4,20}$/;
2 Count must be 0 to 2, which in javascript is .match().length.
var 2ndregex = /([._-])/;
if(myText.match(1stregex) && myText.match(2ndregex).length <=2)
{ isvalid=true; }
I'm using this /[-\+,\.0-9]+/ to match numbers in strings like +4400,00 % or -3500,00 % or 0.00 %.
The matched results I want is +4400,00 and I correctly get it.
What if I wanted the same results for a string like +4.400,00 % (dot for thousands) ?
EDIT
How do I have to modify my RegEx for matching numbers in strings like <font color="red">+44.500 %</font>?
/[\-\+]?\s*[0-9]{1,3}(\.[0-9]{3})*,[0-9]+/
That should cover strings that
may start with a + or -, and then perhaps some whitespaces
then have between one and three numbers
then have groups of three numbers, prefixed with a period
then have a comma and at least one number behind the comma
Regarding your additional question (matching numbers inside strings), you should look into the manual of whatever regex API you're using. Most APIs have separate search and match methods; match wants the whole string to be part of your regular expression's language, while search will also match substrings.
[\+-]? - plus or minus
\d{1,3} - some digits
(\.\d{3})* - groups of 3 digits with point before
,\d{2} comma and 2 more digits
And so we get:
/[+-]?\d{1,3}(\.\d{3})*,\d{2}/
Your regex will already match ".". But it sounds like you also want to strip "." out? if that's the case, you need a substiution. In Perl,
if ($input =~ /(-|\+)[0-9][,\.0-9]+/) {
$input =~ s/\.//;
} else {
die;
}
I've also changed the regex so it will only match - and + at the start, and so it requires an initial digit