RegEx exact repetition is not working - regex

I am a noob in RegEx and I am trying to write a RegEx pattern that has a minimum of 6 and maximum of 9 total characters, where the first 3 characters are letters (case-insensitive, alpha only) and the rest are digits.
I have the following pattern: ^\w{3}\d{3,6}$
But for some reason, that pattern returns true when I enter the following: aa12345 or Ap4587 and so on. I need that the first 3 characters are only letters (exact).
I hope someone will be able to help me on this.
Thanks!!!

\w is equivalent to [a-zA-Z0-9_]. You should change the regex to:
^[a-zA-Z]{3}\d{3,6}$

Use [a-zA-Z] for only alphabets. I prefer using [0-9] even it's same as \d for consistency
/^[a-zA-Z]{3}[0-9]{3,6}$/
\w matches a-z, A-Z, 0-9, _ and should only be used for alphanumeric character

If you want to allow a broader range of unicode values, I'd recommend:
[\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Lm}]{3}
This will allow lowercase, uppercase, title, "other" and modifiers as your first three characters.
For example, [a-zA-Z]{3} would exclude the word "Résumé" because of the special characters. The pattern above would allow it.
I recommend you check out the documentation for regular expression character classes:
Character Classes or Character Sets
The MSDN documentation is also very good and most of it is compatible with standard regex libraries:
Character Classes in Regular Expressions

Try this:
^[a-zA-Z]{3}\d{3,6}$
as \w matches a-z, A-Z, 0-9

Related

Regular Expression For Password Field

I need a regular expression for a password field that:
Must have 1 number
Must have 1 letter (uppercase)
Must have 1 letter (lowercase)
Must be at least 8 characters in length
Must only contain alpha and numeric characters
So far I have:
((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,})
This meets most of my conditions above. But how can I limit this to only allow alpha numeric characters.
Use [a-zA-Z0-9] instead of . and anchor your regex:
^((?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9]{8,})$
You can change the base at the end:
((?=.*\d)(?=.*[a-z])(?=.*[A-Z])[^\W_]{8,})
This solution expects your regex engine to be anchored. If not, anchor them with ^$.
[^\W_] is negated character class. It asserts that this character is not a word character or _.
As word characters covers alphanumeric characters and underscores, this double-negated character class shorthand [^\W_] is well-used for these scenarios.
You can use [[:alnum:]] as well, if your regex engine supports ascii classes.
Here is a regex demo!

regex included a-zA-Z, digit, and some symbol with length limit

I try to create a regex to match lower and uppercase of A-Z, digits and ##$_ symbols with length limit of 4 to 16 for all of string.
My useless regex:
/^([a-zA-Z])|(\d)|(##\$_){4,16}$/
I test Online regex generators Like http://www.jslab.dk/tools.regex.php but don't have a good result .
Your regex /^([a-zA-Z])|(\d)|(##\$_){4,16}$/ matches for a single letter OR a single digit OR 4 to 16 characters of "##\$_".
The groups around the alternatives are useless.
One solution would be to make a group around the whole alternation
/^([a-zA-Z]|\d|##\$_){4,16}$/
but the better solution would be to add everything to one character class
/^[a-zA-Z##$_\d]{4,16}$/
See it here on Regexr
you can maybe simplify it further, since [a-zA-Z\d_] is the same than \w, when \w is not unicode based!
/^[\w##$]{4,16}$/
\w includes lowercase and UPPERCASE letters, digits and the _ character
RegEx Pattern: ^[\w#\#\$]{4,16}$
Explained demo here: http://regex101.com/r/rK1yH2
The expression that you need is this one:
( ([a-zA-Z])|(\d)|(##\$_) ){4,6}
The problem that you have in yours is that the last {2,6} are affecting only to the last group of brackets, not to the whole expression. Also make sure that the "/^" and "$/" are mandatory for your case, because the "^" means "not", so I'm not sure why you have it there.
You can also see it graphically here: http://www.debuggex.com/

Regex help NOT a-z or 0-9

I need a regex to find all chars that are NOT a-z or 0-9
I don't know the syntax for the NOT operator in regex.
I want the regex to be NOT [a-z, A-Z, 0-9].
Thanks in advance!
It's ^. Your regex should use [^a-zA-Z0-9]. Beware: this character class may have unexpected behavior with non-ascii locales. For instance, this would match é.
Edited
If the regexes are perl-compatible (PCRE), you can use \s to match all whitespace. This expands to include spaces and other whitespace characters. If they're posix-compatible, use [:space:] character class (like so: [^a-zA-Z0-9[:space:]]). I would recommend using [:alnum:] instead of a-zA-Z0-9.
If you want to match the end of a line, you should include a $ at the end. Turning on multiline mode is only when your match should extend across multiple lines, and it reduces performance for larger files since more must be read into memory.
Why don't you include a copy of sample input, the text you want to match, and the program you are using to do so?
It's pretty simple; you just add ^ at the beginning of a character set to negate that character set.
For example, the following pattern will match everything that's not in that character set -- i.e., not a lowercase ASCII character or a digit:
[^a-z0-9]
As a side note, some of the more helpful Regular Expression resources I've found have been this site and this cheat sheet (C# specific).
Put at ^ at the begining of your character class expression: [^a-z0-9]
At start [^a-zA-Z0-9]
for condition;
pre_match();
pre_replace();
ergi();
try this
You can also use \W it's a shorthand for non-word character (equal to [^a-zA-Z0-9_])

What is the regex to match an alphanumeric 6 character string?

I need regex for asp.net application to match an alphanumeric string at least 6 characters long.
I’m not familiar with ASP.NET. But the regular expression should look like this:
^[a-zA-Z0-9]{6,}$
^ and $ denote the begin and end of the string respectively; [a-zA-Z0-9] describes one single alphanumeric character and {6,} allows six or more repetitions.
I would use this:
^[\p{L}\p{N}]{6,}$
This matches Unicode letters (\p{L}) and numbers (\p{N}), so it's not limited to common letters the Latin alphabet.
^\w{6,}$ ^[a-zA-Z0-9]{6,}$
(Depending on the Regex implementation)
Note, that \w also matches _!

Regex for alphanumeric, but at least one letter

In my ASP.NET page, I have an input box that has to have the following validation on it:
Must be alphanumeric, with at least one letter (i.e. can't be ALL
numbers).
^\d*[a-zA-Z][a-zA-Z0-9]*$
Basically this means:
Zero or more ASCII digits;
One alphabetic ASCII character;
Zero or more alphanumeric ASCII characters.
Try a few tests and you'll see this'll pass any alphanumeric ASCII string where at least one non-numeric ASCII character is required.
The key to this is the \d* at the front. Without it the regex gets much more awkward to do.
Most answers to this question are correct, but there's an alternative, that (in some cases) offers more flexibility if you want to change the rules later on:
^(?=.*[a-zA-Z].*)([a-zA-Z0-9]+)$
This will match any sequence of alphanumerical characters, but only if the first group also matches the whole sequence. It's a little-known trick in regular expressions that allows you to handle some very difficult validation problems.
For example, say you need to add another constraint: the string should be between 6 and 12 characters long. The obvious solutions posted here wouldn't work, but using the look-ahead trick, the regex simply becomes:
^(?=.*[a-zA-Z].*)([a-zA-Z0-9]{6,12})$
^[\p{L}\p{N}]*\p{L}[\p{L}\p{N}]*$
Explanation:
[\p{L}\p{N}]* matches zero or more Unicode letters or numbers
\p{L} matches one letter
[\p{L}\p{N}]* matches zero or more Unicode letters or numbers
^ and $ anchor the string, ensuring the regex matches the entire string. You may be able to omit these, depending on which regex matching function you call.
Result: you can have any alphanumeric string except there's got to be a letter in there somewhere.
\p{L} is similar to [A-Za-z] except it will include all letters from all alphabets, with or without accents and diacritical marks. It is much more inclusive, using a larger set of Unicode characters. If you don't want that flexibility substitute [A-Za-z]. A similar remark applies to \p{N} which could be replaced by [0-9] if you want to keep it simple. See the MSDN page on character classes for more information.
The less fancy non-Unicode version would be
^[A-Za-z0-9]*[A-Za-z][A-Za-z0-9]*$
^[0-9]*[A-Za-z][0-9A-Za-z]*$
is the regex that will do what you're after. The ^ and $ match the start and end of the word to prevent other characters. You could replace the [0-9A-z] block with \w, but i prefer to more verbose form because it's easier to extend with other characters if you want.
Add a regular expression validator to your asp.net page as per the tutorial on MSDN: http://msdn.microsoft.com/en-us/library/ms998267.aspx.
^\w*[\p{L}]\w*$
This one's not that hard. The regular expression reads: match a line starting with any number of word characters (letters, numbers, punctuation (which you might not want)), that contains one letter character (that's the [\p{L}] part in the middle), followed by any number of word characters again.
If you want to exclude punctuation, you'll need a heftier expression:
^[\p{L}\p{N}]*[\p{L}][\p{L}\p{N}]*$
And if you don't care about Unicode you can use a boring expression:
^[A-Za-z0-9]*[A-Za-z][A-Za-z0-9]*$
^[0-9]*[a-zA-Z][a-zA-Z0-9]*$
Can be
any number ended with a character,
or an alphanumeric expression started with a character
or an alphanumeric expression started with a number, followed by a character and ended with an alphanumeric subexpression