Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python regex - identify String with more than N non-alphaNumerics

Tags:

python

regex

I want to identify strings with more than 3 non-alphanumerics (except \.\- \s) in it, which counts punctuations & special characters.
I've tried with below regex but it identifies strings with "non-alphanumerics in sequence"

([^a-zA-Z0-9.-\s]){2,}

eg: abc__def is identified, ab_c_d is not identified

I want to identify any string which has more than 3 non-alphanumerics (except \.\- \s)

like image 596
Denver Avatar asked Apr 10 '26 15:04

Denver


1 Answers

If you want strings to be identified in which at least three (as suggested by your examples) non-alphanumerics occur, not necessarily in sequence, you can use:

([^a-zA-Z0-9.\-\s].*){3}

You can simply use {3} because .* at the end will take care of the remainder of the string.

like image 195
Tim Zimmermann Avatar answered Apr 12 '26 03:04

Tim Zimmermann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!