I am new to regex and trying to find a regex, which will allow Alphanumeric or text but not complete numeric with special characters like "," , "'","()" ,"/" ,".".
ex: aa12,--allowed
aa,-allowed
123-Not allowed
123,--not allowed
123,a--allowed.
Alpha Numeric-
\d*[a-zA-Z][a-zA-Z\d\s]*$
is working but stuck with including special characters. Any help?
If you simply want to make sure the string contains a character, just match with
.*[a-zA-Z].*
if you need to match the full line. Or even simpler
[a-zA-Z] or [a-z]
The last if you can set the case insensitive flag (you haven't stated regex flavor). These last two will simply match if there's a letter in the text. And I can't see that what you're looking for is anything but that.
See it here at regex101.
This works for me in all scenarios..
/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[_#.#&+-])[A-Za-z0-9_#.#&+-]{8,}$/
Here is how it works,
^(?=.*\d) - checks for at-least one digit in string
(?=.*[a-z]) - checks for at-least one lowercase in string
(?=.*[A-Z]) - checks for at-least one uppercase in string
(?=.*[_#.#&+-]) - checks for at-least one special character in string
{8,} - checks the string length for a minimum of 8 characters
Click here to test it out at Regex 101.
Probably I couldn't convey the issue am trying to sort it out. My case is
If All Numbers--Error
If All Special Characters--Error
If only Numbers + special Characters--Error
Remaining all should be flaged as non error. For the special characters, I get a specific list to let them in. I tried & tried finally Below Regex seems working. Any Optimizations?
\d*([a-zA-Z,/().][a-zA-Z][,/().])[a-zA-Z\d\s]*$
Related
I'm working on some legacy code, found a regex and was hoping someone could dissect it for me:
((?=.*[^a-zA-Z])(?=.*[a-z])(?=.*[A-Z]).{8,})
I know what it's doing, but not entirely sure how it's doing it. What exactly is the "." doing after the conditions?
I'm trying to add some code to generate this regex dynamically. That is, if a bunch of configuration says the password can do anything, I want to return a regex that will return true for ALL CALLS OF .matches() in groovy. So far I've just done string formatting, so the regex created is "(.)", however this is returning false when called with matches().
Thanks for the help!
Here's the explanation broken down for you and a couple example strings.
http://regex101.com/r/oZ6dK4
the tl;dr here, is using the lookahead assertions, it's requiring that at least somewhere in the string you have:
a lowercase letter [a-z]
an uppercase letter [A-Z]
a NON-letter [^a-zA-Z]
and that it must be at least 8 characters {8,}
This looks like a password requirements validator to me.
Strings it will match:
asdfsdklj2-3049-09AS
09809LK2JL23Lsdf
Strings it won't match:
asdfsdf
2398-02934
23Abs
As for your question about the dot (.): it's not a period, it's a regex special character that matches anything except newline. (In the regex101 explanation you can see it states .{8,} matches any character (except newline)). In this case, the reason ~"(.)".matches() returns false, is because it requires a minimum of 8 characters in order to validate.
This checks to ensure the password contains a non-alphabetic character, that it contains at least one lowercase and uppercase alphabetic character, and finally that the entire string is at least 8 characters in length. The latter portion is where the . comes in – matching the entire string and ensuring it's 8+ characters.
(?=.*[^a-zA-Z) - this checks that there is a non-alpha character somewhere in the password
(?=.*[a-z]) - this checks that there is a lower case character in the password somewhere
(?=.*[A-Z]) - this checks that there is an upper case character in the password somewhere
.{8,} - this checks that there are at least 8 characters.
?= is the positive lookahead. The regex in this bracket must be matched and it must occur after this spot in the input.
Check rexegg.com for all kinds of useful regex information and a quick reference that's very useful.
I need a single regex to check if input must not be empty plus the input has alphanumeric characters only.
I know the alphanumeric part,^[\s+0-9a-zA-Z]+$, but I am not sure about the not empty requirement.
I can only use a single expression and I can't use any language function.
Simply use this regex to match a non-empty alphanumeric string:
^[a-zA-Z0-9]+$
Details
^ - string start
[a-zA-Z0-9]+ - one or more letters or digits
$ - string end.
I'm going to assume by Not empty you mean not only white space, otherwise you've got the answer you want. + means one or more.
^[a-zA-Z0-9][a-zA-Z0-9\s]*^
will make sure that the string has something other than white space in it.
Additionally if \s is valid then I assume \w is as well, meaning that this could more easily be said as
^[(?:\w|\s)*$
The ?: in the ( ) makes it a non-capture group. If you don't care about capture then this can be omitted, making it the very terse.
^\w(\w|\s)*$
1- I'm planning to use the regEx to validate user first and last name inputs using this regex:
/^[a-zA-ZàáâäãåèéêëìíîïòóôöõøùúûüÿýñçčšžÀÁÂÄÃÅÈÉÊËÌÍÎÏÒÓÔÖÕØÙÚÛÜŸÝÑßÇŒÆČŠŽ∂ð ,.'-]+$/u
However I don't want to allow underscore "_", no only empty space (cannot be left blank) and at least 2 characters. How can I appy them to the regEx above ?
2- For my strong password input validation, I need it be of minimum 8 character length
and it should consist of at least one letter and non-letter ( For e.g. qsgtest123, qsgtest!##)
I will be grateful if you help me with these 2 regExs.
Have a try with:
/^[\p{L},.'-]+[\p{L} ,.'-]*[\p{L},.'-]+$/u
/^((?!_)[a-zA-ZàáâäãåèéêëìíîïòóôöõøùúûüÿýñçčšžÀÁÂÄÃÅÈÉÊËÌÍÎÏÒÓÔÖÕØÙÚÛÜŸÝÑßÇŒÆČŠŽ∂ð ,.'-])+$/u
The above should apply to your first question.
This for the name
/^(?! +$)[a-zA-ZàáâäãåèéêëìíîïòóôöõøùúûüÿýñçčšžÀÁÂÄÃÅÈÉÊËÌÍÎÏÒÓÔÖÕØÙÚÛÜŸÝÑßÇŒÆČŠŽ∂ð ,.'-]{2,}$/u
The only difference is the "at least 2 characters" at the end and (?! +$) that means "fail if there are only spaces and end of the string".
Tester: http://gskinner.com/RegExr/?2uv74
And this one for the password:
/^(?=.*[a-zA-ZàáâäãåèéêëìíîïòóôöõøùúûüÿýñçčšžÀÁÂÄÃÅÈÉÊËÌÍÎÏÒÓÔÖÕØÙÚÛÜŸÝÑßÇŒÆČŠŽ∂ð])(?=.*[^a-zA-ZàáâäãåèéêëìíîïòóôöõøùúûüÿýñçčšžÀÁÂÄÃÅÈÉÊËÌÍÎÏÒÓÔÖÕØÙÚÛÜŸÝÑßÇŒÆČŠŽ∂ð]).{8,}$/u
(I'm using your definition of "letter" :-) ). It means:
look forward if present any character any number of times followed by a "letter"
look forward if present any character any number of times followed by a "non-letter"
(these two look forward don't "move" the regex cursor, that is still at the first character)
match any character 8 or more times
I see you are using the /u at the end of the regex. You are probably using Perl. To match any letter you should use \p{L} (and to match any non-letter you should use \P{L}) instead of writing long lists of characters. So the first one would become:
/^(?! +$)[\p{L} ,.'-]{2,}$/u
and the password one:
/^(?=.*\p{L})(?=.*\P{L}).{8,}$/u
And we will ignore the composable diacritics of Unicode :-)
Unless you'd prefer to include them... Then
/^(?! +$)(?=.{2,})(\p{L}\p{M}*|[ ,.'-])*$/u
(we pre-check the absence of all-spaces and the minimum length, and then we check that all the string is composed of letters (each one with an optional zero or more combining mark) or the other symbols in the [])
I have a regex that I thought was working correctly until now. I need to match on an optional character. It may be there or it may not.
Here are two strings. The top string is matched while the lower is not. The absence of a single letter in the lower string is what is making it fail.
I'd like to get the single letter after the starting 5 digits if it's there and if not, continue getting the rest of the string. This letter can be A-Z.
If I remove ([A-Z]{1}) +.*? + from the regex, it will match everything I need except the letter but it's kind of important.
20000 K Q511195DREWBT E00078748521
30000 K601220PLOPOH Z00054878524
Here is the regex I'm using.
/^([0-9]{5})+.*? ([A-Z]{1}) +.*? +([A-Z]{1})([0-9]{3})([0-9]{3})([A-Z]{3})([A-Z]{3}) +([A-Z])[0-9]{3}([0-9]{4})([0-9]{2})([0-9]{2})/
Use
[A-Z]?
to make the letter optional. {1} is redundant. (Of course you could also write [A-Z]{0,1} which would mean the same, but that's what the ? is there for.)
You could improve your regex to
^([0-9]{5})+\s+([A-Z]?)\s+([A-Z])([0-9]{3})([0-9]{3})([A-Z]{3})([A-Z]{3})\s+([A-Z])[0-9]{3}([0-9]{4})([0-9]{2})([0-9]{2})
And, since in most regex dialects, \d is the same as [0-9]:
^(\d{5})+\s+([A-Z]?)\s+([A-Z])(\d{3})(\d{3})([A-Z]{3})([A-Z]{3})\s+([A-Z])\d{3}(\d{4})(\d{2})(\d{2})
But: do you really need 11 separate capturing groups? And if so, why don't you capture the fourth-to-last group of digits?
You can make the single letter optional by adding a ? after it as:
([A-Z]{1}?)
The quantifier {1} is redundant so you can drop it.
You have to mark the single letter as optional too:
([A-Z]{1})? +.*? +
or make the whole part optional
(([A-Z]{1}) +.*? +)?
You also could use simpler regex designed for your case like (.*)\/(([^\?\n\r])*) where $2 match what you want.
here is the regex for password which will require a minimum of 8 characters including a number and lower and upper case letter and optional sepecial charactor
/((?=.\d)(?=.[a-z])(?=.*[A-Z])(?![~##$%^&*_-+=`|{}:;!.?"()[]]).{8,25})/
/((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?![~##\$%\^&\*_\-\+=`|{}:;!\.\?\"()\[\]]).{8,25})/
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