FutureWarning: Possible nested set at position 1 Error Python

I was working on something and at some point, I needed to check whether the string satisfies this:
The string must contain at least 5 words and each separated by a hyphen(-) or an underscore(_).
Here is the code that I wrote:

password=eval(input('Password:'))
pattern=r'[[\w][-_]]{5,}'
import re
re.fullmatch(pattern,password)

But it gives ‘ ipython-input-32-7c87b09218f8>:4: FutureWarning: Possible nested set at position 1
re.fullmatch(pattern,password) ‘ error. Why that happens, any idea?Thanks in advance.Btw I’m using Jupyter notebook.

>Solution :

You can match 1+ word characters, and then repeat at least 4 times matching either _ or / and again 1 or more word characters.

\w+(?:[/_]\w+){4,}

Explanation

  • \w+ Match 1+ word characters
  • (?: Non capture group to repeat as a whole part
    • [/_] Character class matching either / or _
    • \w+ Match 1+ word characters
  • ){4,} close the no capture group and repeat 4 or more times

See a regex demo.

Leave a Reply