4 ms·
Haven't tried it since I'd rather not sign up for an account. But regarding this example: Minimum eight characters, at least one letter, one number and one
by Cyberdog 4y ago
Haven't tried it since I'd rather not sign up for an account. But regarding this example:
Minimum eight characters, at least one letter, one number and one special character
>> ^(?=.*[A-Za-z])(?=.*d)(?=.*[@$!%*#?&])[A-Za-zd@$!%*#?&]{8,}$
I assume the "d" characters here are supposed to be "\d" but the backslashes wandered off somewhere. Not sure if that's just in the example or the actual output from the AI. Some of those special characters like "*" and "$" need a backslash too.
Also, this pattern will not allow any character which is not a letter, number, or in that very finite list of special characters - so no spaces, no carets, no quote marks or apostrophes, etc. Not allowing certain printable characters in a password is a bad practice (I won't complain too much if you forbid the non-printable ones).
- asicsp 4y agoAll words starting with the letter "n" and ending with "g", case insensitive >> /^nw*g$/i Yeah, seems like backslash got gobbled up in some sort of parsing before what gets displayed on the site. Should be `\w` here. Regarding your other point, characters like `*` won't need escaping within character class. Not sure why only those particular set of characters are deemed "special characters" - why not `-`, `=`, parentheses, curly braces, etc? Also, the site should specify which regex flavor is being generated, I'd guess JS. All said, it is good to see tools to help with regex, as long as the solutions will be tested to make sure it fits their particular problem.
- Cyberdog 4y ago> Regarding your other point, characters like `*` won't need escaping within character class. Really? Huh. I've been escaping them anyway, and I think I'll continue to do so just to avoid ambiguity when reading patterns. I was about to end this post with "an extra backslash never hurt anybody" but then I realized that this is HN and writing that would surely prompt someone to reply with some bizarre case where an extra backslash caused a cascade of failures which cost a company fifteen million dollars and/or killed 43 people.
- asicsp 4y agoHaha, you are right to be skeptical. For example, `GNU grep` doesn't treat backslash as special within character class. You need to place metacharacters like `-`, `^`, `]`, etc in particular spots to match them literally.
- hddqsb 4y agoObligatory example where the extra backslash makes a difference: $ printf '%s\n' 'asterisk: *' 'backslash: \' | grep '[*]' asterisk: * $ printf '%s\n' 'asterisk: *' 'backslash: \' | grep '[\*]' asterisk: * backslash: \ It depends on the regex flavour though (doesn't happen with Python, JavaScript, or Perl for example).
- deleted 4y ago[deleted]
- xdfgh1112 4y agoSerious answer: some people use character classes as a more readable form of escape, e.g. [.] So escaping inside would be extra redundant.
- mhitza 4y agoThat regex doesn't do what the description above it says, with or without a backslash.
- Daneel_ 4y agoHow so? Start of line “^”, then an “n”, then any word characters zero or more times (\w*), then a “g”, then the end of the line “$”. The surrounding slashes are convention for certain regex languages, and the last “i” turns on the case insensitive flag. Personally I’d write this as “(?i)\bn[a-z]*g\b” but there’s many approaches for many different situations.
- ukd1 4y agoI assume mostly using start of line instead of \b, and expecting w only to be zero+ times in the middle.
- y4mi 4y agoBecause i struggled to understand what you meant I'd like to rephrase it: It matches only if the regex is applied to a singular word. It's not going to match if there is a sentence or any apostrophe etc, which is implied to be valid input because it supposedly matches "all words".
- Filligree 4y agoThat’s easy to misunderstand. And if humans have trouble with it, then I won’t blame the poor AI. Getting an AI to ask clarifying questions would be useful, of course…
- OscarDC 4y agoI guess it doesn't work for example when words contain dashe(s) or apostrophe(s) which might exist (for a single indivisible word) in some languages.
- thrdbndndn 4y agoNo, you don't need to escape these special characters if they're in []. For JS (which is used here) or Python at least. Some other implementations may vary.