0

How can I use regex to check if the string contains chars, number and special character for password input?

abc => false abc12 => false abc12#@ => true

Luqman Jr
  • 69
  • 1
  • 5
  • 2
    A regular expression isn't a good solution for this problem because you have to deal with 3!=6 possible orderings of (char, number, special). Maybe `strings.ContainsFunc(s, unicode.IsLetter) && strings.ContainsFunc(s, unicode.IsDigit) && strings.ContainsFunc(s, isSpecial)` -- where you have to define `isSpecial` yourself based on what you call special. – Paul Hankin Aug 12 '23 at 10:35
  • @PaulHankin, I'm not familiar with golang but [I see](https://pkg.go.dev/github.com/s-kozlov/goback/regexp/syntax) its regex engine supports positive lookaheads, so couldn't one attempt to match `^(?=.*[a-zA-Z])(?=.*\d)(?=.*[#@^$...])` which avoids the combinatorial issue you mention? The character class `[#@^$...]` enumerates all "special characters", however they are defined. – Cary Swoveland Aug 12 '23 at 19:16
  • Luqman Jr, I assume you mean "contains a letter, number..." rather than "contains a char, number...". Please edit to correct. I expect the vote to close may due to the fact that your question is a duplicate of an earlier question but it could also be due to the fact that you did not show what you tried or describe efforts you made to solve the problem, which SO requires for questions that are homework problems. (It's not clear, of course, if your question is a homework problem.) – Cary Swoveland Aug 12 '23 at 19:25
  • Your question is vague and imprecise. First, to the extent I understand your question, you shouldn't be trying to solve this using a single regex unless the set of characters has to occur in a specific sequence. You should instead be using a sequence of tests to verify that at least one character of each mandatory set of characters is present. You should have also shown us your attempt at a solution. StackOverflow exists to help you fix your code -- not write your code for free. – Kurtis Rader Aug 13 '23 at 06:06
  • 1
    @CarySwoveland: You're missing the point made by Paul Hankin and the "already has answers here" link. The O.P. doesn't say so but usually the constraint is one or more instances of each class of characters in any order. So using a positive lookahead by itself is insufficient. Trying to do this using a single regex is possible but incredibly obtuse and inefficient. While I love regexes it is often the case, as in this question, that they are the wrong tool for the job. – Kurtis Rader Aug 13 '23 at 06:27
  • @Kurtis, I penned my comments before the question was closed, but no matter, as I've seen dozens of variants of this question, assuming I understand it correctly, namely, that there is no requirement concerning the order of characters. In that case I don't understand Paul's reference to combinations of orderings. With regards to the simple regex I suggested, I assume you are aware that each of a sequence of positive lookaheads, with the first anchored to the beginning of the string, does not move the string pointer beyond the beginning of the string. – Cary Swoveland Aug 13 '23 at 08:06

0 Answers0