Verify string containing certain characters using regular expression in tcl [duplicate] - regex

This question already has an answer here:
Regex for Password: "Atleast 1 letter, 1 number, 1 special character and SHOULD NOT start with a special character"
(1 answer)
Closed 7 years ago.
I have a requirement in tcl where we need to accept a string from the user. The string must consist of at least 1 capital letter, 1 number and 1 special character.
Can anyone say what regular expression we should use?

If your assignment states that you have to solve it in one go, sort the string's characters first. Then use a regular expression that looks for the kinds of characters you want in the order digit-uppercase-lowercase, allowing for other characters in between.
If you don't need to solve it with one application of regexp, test three times, one for each of the kinds of character you want. If all tests succeed, you have a strong password.

Related

RegEx to find count of special characters in String [duplicate]

This question already has answers here:
How to get the count of only special character in a string using Regex?
(6 answers)
Closed 2 years ago.
I need to form the RegEx to produce the output only if more than two occurrences of special characters exists in the given string.
1) abcd##qwer - Match
2) abcd#dsfsdg#fffj-Match
3) abcd#qwetg- No Match
4) acwexyz - No Math
5) abcd#ds#$%fsdg#fffj-Match
Can anyone help me on this?
Note: I need to use this regular expression in one of the existing tool not in any programming language.
UPDATE after OP edit
The edited OP introduces a small amount of additional complexity that necessitates a different pattern entirely. The keys here are that (a) there is now a significantly limited set of "special characters" and (b) that these characters must appear at least twice (c) in any position in the string.
To implement this, you would use something like:
(?:.*?[##$%].*?){2,}
Asserts a non-capturing group,
Which contains any number of characters, followed by
Any character in the set ##$%
Followed by any number of characters
Ensures this pattern happens twice in a given string.
Original answer
By "special characters", I assume you mean anything outside standard alphanumeric characters. You can use the pattern below in most flavors of Regex:
([^A-Za-z0-9])\1
This (a) creates a set of all characters not including alphanumeric characters and matches a character against it, then (b) checks to see if the same character appears adjacent.
Regex101

Confusion in JavaScript RegExp ?= Quantifier [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 5 years ago.
What the difference between
(?=.\d)(?=.[a-z])(?=.[A-Z])
and
(.\d)(.[a-z])(.[A-Z])
When I test the string a2A only the first RegExp returns true. Can anyone explain this for me?
The difference is in the lookahead operator for each of the terms in the regex. The LA operator matches the sub-regex it guards as usual, but effectively locks the initial matching position for the subsequent regex portion.
This means that the first regex should not match (contrary to your tests, which engine have you used ?) - Given any initial matching position, the second character would have to be a number, a lowercase letter, and an uppercase letter, all at the same time.
Observe that this will not happen if the . ('any char') is quantified:
(?=.*\d)(?=.*[a-z])(?=.*[A-Z])
Each LA term may skip an arbitrary amount of material before matching the character class, and this amount may differ between the subexpressions.
The second alternative (with and without quantification) will never match as it invariably requires a subsequence of digit-letter-letter, which the test string a2A does not provide.

How to build a regular expression which prohibits hyphens from appearing at the start and end of a string? [duplicate]

This question already has answers here:
RegEx for allowing alphanumeric at the starting and hyphen thereafter
(4 answers)
Closed 5 years ago.
I want to build a regular expression which only matches [A-Za-z0-9\-] with an additional rule that hyphens (-) are not allowed to appear at the start and at the end.
For example:
my-site is matched.
m is matched.
mysite- is not matched.
-mysite is not matched.
Currently, I've come up with ^[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9]+$.
But this doesn't match m.
How can I change my regular expression so that it fits my needs?
Use look arounds:
^(?!-)[A-Za-z0-9-]*(?<!-)$
The reason this works is that look arounds don't consume input, so the look ahead and the look behind can both assert on the same character.
Note that you don't need to escape the dash within the character class if it's the first or last character.

how to perform regex for password validation without special character? [duplicate]

This question already has answers here:
Regex to validate password strength
(11 answers)
Closed 6 years ago.
Regex for password of minimum length-7 without special characters , atleast one uppercase and one number .
In my case, regex which satisfies:
Killer1 - atleast one uppercase (K), atleast one number (1) , minumum length - 7
Melbourne123- valid
London24 - valid
Thanks in advance.
Minimum length 7
This part is unsurprisingly the simplest. You can just use:
.{7,}
In order to perform the other checks in a single regex, you need to make use of look-aheads as follows:
at least one upper-case
(?=.*[A-Z])
at least one number
(?=.*\d)
without special characters
I would strongly advise against this requirement if at all possible. Adding this does not improve your security, and will only frustrate your users. But, if you really must, then:
(?!.*[^a-zA-Z0-9])
(Modify the above as appropriate -- depending on what exactly you mean by "special" characters.)
Putting this all together into a single pattern, the final answer is:
\A(?=.*[A-Z])(?=.*\d)(?!.*[^a-zA-Z0-9]).{7,}
You could also simplify this regex slightly, by merging the "no special chars" and "minimum length" requirements into a single regex condition as follows:
\A(?=.*[A-Z])(?=.*\d)[a-zA-Z0-9]{7,}\z
(Note the additional use of a \z anchor here, in order to check that all password characters are in the whitelisted "non-special" characters.

Regular expression for 10 numeric digits [duplicate]

This question already has an answer here:
Java Regex for telephone number - Must Include only 8 digits with not more than 2 dash [duplicate]
(1 answer)
Closed 7 years ago.
10 numeric digits, may be in the following formats: 123-4-567890, 1234-567890 or 1234567890
What is the regular expression for above digits?
Any help is appreciated.
Thanks.
Assuming you mean any digit, 0-9, should be found if (and only if) it meets the three formats you presented, one regex that would work is
(([0-9]{3}-[0-9]{1}-[0-9]{6})|([0-9]{4}-[0-9]{6})|([0-9]{10}))
The above breaks down to three separate patterns, one for each case you presented, separated by regex's equivalent of "or", the | character. Each of the statements above contains [0-9], a character class which will match any digit. Following each character class is a {n} statement, which means "repeat the previous item n times".
Disclaimer, there is probably a cleverer way to do this with a shorter pattern, but my regex-foo isn't quite that advanced yet