Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Posix classes in regex module Python

Tags:

python

regex

I installed the module regex (not re!) for Python 3.4.3 solely to be able to use POSIX classes such as [:graph:]. However, these don't seem to work.

import regex

sentence = "I like math, I divided ÷ the power ³ by ¾"

sentence = regex.sub("[^[:graph:]\s]","",sentence)

print(sentence)

Output: I like math, I divided ÷ the power ³ by ¾

Expected output: I like math, I divided the power by

It does work in PCRE though. So what am I missing here?

like image 658
Bram Vanroy Avatar asked Jan 21 '26 08:01

Bram Vanroy


1 Answers

try sentence = regex.sub("[^[:graph:]\s]","",sentence,flags=regex.VERSION1)

You need to add flag regex.VERSION1

like image 182
vks Avatar answered Jan 23 '26 20:01

vks