0

Python regex that will match anything that isn't a alphabet letter.

3rd Jun 2021, 4:06 PM
David Holmes Ng'andu
David Holmes Ng'andu - avatar
4 Answers
+ 3
^a-zA-Z
3rd Jun 2021, 4:20 PM
Bibek Oli
Bibek Oli - avatar
+ 1
your attempt?
3rd Jun 2021, 4:09 PM
Rellot's screwdriver
Rellot's screwdriver - avatar
0
Rellot's screwdriver I wrote this Import re old_str = "da8v+i4d" pattern = r'[0-9]' new_str = re.sub(pattern, '', old_str) print(new_str[::-1]) #that will match all numbers and replace them with an empty string, then Reverse the string right. #but i also want it to match the special symbols
3rd Jun 2021, 4:26 PM
David Holmes Ng'andu
David Holmes Ng'andu - avatar
0
Bibek Oli is almost right, except he doesn't explicitly explain than its code should replace your 0-9 character range inside square brackets of your pattern ;) the ^ (carret) at start of a character range is not negate it, so will match any character that are NOT listed after, rather than matching those wich are listed however, you could make your regex replacement a few more efficient, by appending a + operator quantifier after the closing bracket: this will match one to any number of characters defined by the range, and so replace contiguous ones at once instead of replacing each one at a time ;P pattern = '[^a-zA-Z]' r before string is not required, as there's no ambiguous character in your regexp (antislashes), but may be put anyway (r doesn'r stand for regexp string but for raw string: doesn't use escape codes) also, by using case insensitive modifier to your regexp, you could short a few the raw string ^^
3rd Jun 2021, 7:08 PM
visph
visph - avatar