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

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

Related

negation classes regex [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 5 years ago.
i wrote this regex for tokenize a text: "\b\w+\b"
but someone suggets me to convert it into \b[^\W\d_]+\b
can anyone explaing to me why this second way (using negation) is better?
thanks
The first one matches all letters, numbers and the underscore. Depending on the regex engine, this may include unicode letters and numbers. (the word boundaries are superfluous in this case btw.)
The second regex matches only letters (excluding non-word-charcters, digits and the underscore). Due to the word boundary, it will only match them, if they are surrounded by non-word-characters or start/end of th string.
If your regex engine supports this, you might want to use [[:alpha:]] or \p{L} (or [A-Za-z] in case of non-unicode) instead to make your intent clearer.

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.

The use of ".*" in regex for password validation [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 6 years ago.
I came across this regex used for password validation:
(?=.*[a-z])(?=.*[A-Z])(?=.*[\d])(?=.*[^a-zA-Z\d])(?=\S+$).{8,}
There are only two things that are unclear to me about this regex:
what are .* used for and why this regex doesn't work without them?
what is the difference/benefit or using [\d] instead of \d, because the regex works just fine in both cases
.* matches any sequence of characters; . matches any character (other than newline, which is not relevant here) and * matches zero or more of the preceding pattern. This is used in the lookaheads to search for matches anywhere in the password. If you didn't have it,then it would require that you have those types of characters in a specific order: a lowercase letter followed by an uppercase letter followed by a digit. With .*, it means the password must contain at least one of each of them, but they can be anywhere in the password.
There's no difference between \d and [\d]. Whoever write this might just use the brackets out of habit, or perhaps to make it easier to modify it to put other characters into the character class.

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.

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

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.