Two Regular Expression Problems - regex

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 [])

Related

What would be the Regex expression to get the first letter after a group of character and some integers?

I have a string that the following structure:
ABCD123456EFGHIJ78 but sometimes it's missing a number or a character like:
ABC123456EFGHIJ78 or
ABCD123456E or
ABCD12345EFGHIJ78
etc.
That's why I need regular expressions.
What I want to extract is the first letter of the third group, in this case 'E'.
I have the following regex:
(\D+)+(\d+)+(\D{1})\3
but I don't get the letter E.
This seems to work for the example cases you provided.
^(?:[A-Za-z]+)(?:\d+)(.)
It assumes that the first group is only letters and that the second group is only digits.
There's already a nice answer.
But for the records, your initial proposal was very close to work. You just needed to say that the character matching the 3rd group can repeat several times by adding a star:
^(\D+)(\d+)(\D{1})\3*
The main weakness is that \D matches any char except digits, so also spaces. Making it more robust leads us to explicit the range of chars accepted:
^([A-Za-z]+)(\d+)([A-Za-z]{1})\3*
It's much better, but my favourite uses \w to match at the end of the pattern any non white character:
([A-Za-z]+)(\d+)([A-Za-z]{1})\w*

Regex For AlphaNumeric with special chars

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]*$

Match Regular Expressoin if string contains exactly N occrences of a character

I'd like a regular expression to match a string only if it contains a character that occurs a predefined number of times.
For example:
I want to match all strings that contain the character "_" 3 times;
So
"a_b_c_d" would pass
"a_b" would fail
"a_b_c_d_e" would fail
Does someone know a simple regular expression that would satisfy this?
Thank you
For your example, you could do:
\b[a-z]*(_[a-z]*){3}[a-z]*\b
(with an ignore case flag).
You can play with it here
It says "match 0 or more letters, followed by '_[a-z]*' exactly three times, followed by 0 or more letters". The \b means "word boundary", ie "match a whole word".
Since I've used '*' this will match if there are exactly three "_" in the word regardless of whether it appears at the start or end of the word - you can modify it otherwise.
Also, I've assumed you want to match all words in a string with exactly three "_" in it.
That means the string "a_b a_b_c_d" would say that "a_b_c_d" passed (but "a_b" fails).
If you mean that globally across the entire string you only want three "_" to appear, then use:
^[^_]*(_[^_]*){3}[^_]*$
This anchors the regex at the start of the string and goes to the end, making sure there are only three occurences of "_" in it.
Elaborating on Rado's answer, which is so far the most polyvalent but could be a pain to write if there are more occurrences to match :
^([^_]*_){3}[^_]*$
It will match entire strings (from the beginning ^ to the end $) in which there are exactly 3 ({3}) times the pattern consisting of 0 or more (*) times any character not being underscore ([^_]) and one underscore (_), the whole being followed by 0 ore more times any character other than underscore ([^_]*, again).
Of course one could alternatively group the other way round, as in our case the pattern is symmetric :
^[^_]*(_[^_]*){3}$
This should do it:
^[^_]*_[^_]*_[^_]*_[^_]*$
If you're examples are the only possibilities (like a_b_c_...), then the others are fine, but I wrote one that will handle some other possibilities. Such as:
a__b_adf
a_b_asfdasdfasfdasdfasf_asdfasfd
___
_a_b_b
Etc.
Here's my regex.
\b(_[^_]*|[^_]*_|_){3}\b

Regular expression start with specific letter

I am using this ^[S-s][0-9]{4}$ to validate my string, but not working properly. my string has to be in the form of the Letter S (upper-case or lower-case) followed by 4 digits, e.g. S1234. Looks like it works for Letters above S, meaning if I enter w1234 it validates correct, but if I enter a letter below s, like a1234 it doesn’t validate. Thanks.
You need to get rid of the dash:
^[Ss][0-9]{4}$
dashes within [...] denote character ranges. Thus S-s in regex would mean "every character in Unicode character table between S and s" and as those two are not adjacent, you end up with a bunch of matched chars.
Not answer directly the detail content of the question, but whom who end up to this question by the question's title and looking for the answer of regex to find match words begin with specific letter like :
This is a Zone
You should use this regex:
\bd[a-zA-Z]+
[a-zA-Z] should replace by the expected tail you want.
Take a look at this link
[S-s] means the range of all characters between capital S and lowercase s. Try ^[Ss][0-9]{4}$ instead. Or better yet, ^s\d{4}$ with a case-insensitivity modifier (/i in many languages).

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