Pattern grouping doesent work in HTML5 - regex

I try to match this RegExp pattern="([a-zA-Z]+[0-9]*){4,}" which means:
Always start with alphabetic, then if he wants to add a number, all this must be minimum 4; aaaa is validated, but aaa4 is not.
The trick I did is [a-zA-Z]+[a-zA-Z0-9]{4,} so I can oblige the first character to be a letter, then there is at least 4 alphanumeric.

If you want that always start with alphabetic and finish with one or more optional number (must be the minimum 4 elements) your regexp should be
pattern="([a-zA-Z]{4,}\d*|[a-zA-Z]{3,}\d+)"
Although you could want this solution
[a-zA-Z][a-zA-Z0-9]{3,}
In this case you get one alphabet and after three or more alphabetic and numbers characters.

What your current pattern actually does is look for one or more letters followed by 0 or more numbers, and that entire pattern has to repeat a minimum of 4 times.
To get what you want you would need to use a pipe and specify the two different patterns and a negative lookahead:
[a-zA-Z]+[0-9]{4,}|[a-zA-Z]+(?![0-9])
Which means look for a-z one or more times followed by 4 or more digits or look for a-z one or more times and not followed by a digit.
Enter text: <input type="text" pattern="^[a-zA-Z]+[0-9]{4,}|[a-zA-Z]+(?![0-9])">

Related

Regex: How to find a phone number (or number sequence) that begins with a particular single digit (multiple numbers on the same line)

Newbie question but how can I check for instances where there are multiple numbers on the same line. For instance, the content reads for example contact 408-555-5454 or reach out to 408-555-4545. Right now the best I can do is ^4 but that's only catching multiple things if the mutliline flag is tured on. Any idea.
You could try the regex below
/4\d{2}(-| )?\d{3}(-| )?\d{4}/g
This of course assumes that you're looking for numbers that start with 4. You can have a look at the Regex Snippet here and you can experiment with trying different variations of the regex to suit your needs.
here's a key to the regex elements included:
4 = matches the literal number 4
\d{2} = matches 2 digits (0-9).
(-| )? = matches either a hyphen or single space but makes it not required. ie you can have a space or hyphen or not.
\d{3} = matches 3 digits (0-9)
Same as #3 above
\d{4} = matches 4 digits (0-9)
the g flag will ensure that you're searching through the whole text and not stopping after the first match.
If you like the answer please Accept it :)

Different regex conditions on same string

I am trying to implement a regex for phone numbers, based on our business logic.
What the customer wants is that the phone must contain between 8 and 15 characters of numbers, and also can contain any spaces and dots anywhere which doesn't add to the count of numbers. So, theoretically this should be valid:
3 .... 44444444
Because it contains 9 numbers.
I can't really go further on
~[0-9\.\ ]{8,15}$
but obviously it counts dots and spaces to the limit too.
Is it even possible to implement it via regex?
A Regex attempt:
^(?:[ .]*\d){8,15}[ .]*$
This will match 8 to 15 digits, with any number of space or dot happening anywhere in between.
The non-captured group, (?:[ .]*\d), matches any digit preceded by any number of dot or space, {8,15} ensures the range on numbers
[ .]*$ matches any number of dot or space at the end
Demo
As far as I know, regular expressions cannot validate this. However you could maybe globally remove all whitespace and dots and then try to match a regex that is ^[[:digit:]]{8,15}$

Regex - matching while ignoring some characters

I am trying to write a regex to max a sequence of numbers that is 5 digits long or over, but I ignore any spaces, dashes, parens, or hashes when doing that analysis. Here's what I have so far.
(\d|\(|\)|\s|#|-){5,}
The problem with this is that this will match any sequence of 5 characters including those characters I want to ignore, so something like "#123 " would match. While I do want to ignore the # and space character, I still need the number itself to be 5 digits or more in order to qualify at a match.
To be clear, these would match:
1-2-3-4-5
123 45
2(134) 5
Bonus points if the matching begins and ends with a number rather than with one of those "special characters" I am excluding.
Any tips for doing this kind of matching?
If I understood requirements right you can use:
^\d(?:[()\s#-]*\d){4,}$
RegEx Demo
It always matches a digit at start. Then it is followed by 4 or more of a non-capturing group i.e. (?:[()\s#-]*\d) which means 0 or more of any listed special character followed by a digit.
So just repeat a digit, followed by any other sequence of allowed characters 5 or more times:
^(\d[()\s#-]*){5,}$
You can ensure it ends on a digit if you subtract one of the repetitions and add an explicit digit at the end:
^(\d[()\s#-]*){4,}\d$
You can suggest non-digits with \D so et would be something like:
(\d\D*){5,}
Here is a guide.

Regex for Password must contain 8 characters, 2 letter both lower or uppercase letters and one special character ' * ' 5 digit number

I want to create an expression for password as below:
Regex for passwords that must contain 8 characters, start with 2 lower or uppercase letters, contain one special character * and a 5-digit number.
E.g.: az*12345
It must be start with 2 characters;
Contain only single *;
End with 5 digits.
I have tried it with this pattern:
(?=(.*[^a-zA-Z]){2})(?=.*[*]{1})(?=(.*\d){5}).{8}$
However, it yields almost the same results as a regex above. It starts with any character but I want the exact above mentioned pattern. I know I am close to it. Please suggest me what I should do.
If you wish to match just [2-letters]+[*]+[5-digits] pattern, here is what you are looking for:
^[a-zA-Z]{2}\*[0-9]{5}$.

Regex to check for 4 consecutive numbers

Can I use
\d\d\d\d[^\d]
to check for four consecutive numbers?
For example,
411112 OK
455553 OK
1200003 OK
f44443 OK
g55553 OK
3333 OK
f4442 No
45553 No
f4444g4444 No
f44444444 No
If you want to find any series of 4 digits in a string /\d\d\d\d/ or /\d{4}/ will do. If you want to find a series of exactly 4 digits, use /[^\d]\d{4}[^\d]/. If the string should simply contain 4 consecutive digits use /^\d{4}$/.
Edit: I think you want to find 4 of the same digits, you need a backreference for that. /(\d)\1{3}/ is probably what you're looking for.
Edit 2: /(^|(.)(?!\2))(\d)\3{3}(?!\3)/ will only match strings with exactly 4 of the same consecutive digits.
The first group matches the start of the string or any character. Then there's a negative look-ahead that uses the first group to ensure that the following characters don't match the first character, if any. The third group matches any digit, which is then repeated 3 times with a backreference to group 3. Finally there's a look-ahead that ensures that the following character doesn't match the series of consecutive digits.
This sort of stuff is difficult to do in javascript because you don't have things like forward references and look-behind.
Should the numbers be part of a string, or do you want only the four numbers. In the later case, the regexp should be ^\d{4}$. The ^ marks the beginning of the string, $ the end. That makes sure, that only four numbers are valid, and nothing before or after that.
That should match four digits (\d\d\d\d) followed by a non digit character ([^\d]). If you just want to match any four digits, you should used \d\d\d\d or \d{4}. If you want to make sure that the string contains just four consecutive digits, use ^\d{4}$. The ^ will instruct the regex engine to start matching at the beginning of the string while the $ will instruct the regex engine to stop matching at the end of the string.