1

I would like to use the following regular expression in python and it was successfully tested with PCRE. Switching to python is giving me an error.

^password\s+sufficient\s+pam_unix\.so\s(?|.*?(remember=(?P<val>\d*))|()())?

My input file is

password    sufficient pam_unix.so sha512 shadow use_authtok
password    sufficient pam_unix.so sha512 shadow use_authtok remember=2 
#password    sufficient pam_unix.so sha512 shadow use_authtok remember=10 

I'm expecting two matches with group_1 and group_2 each.

Match1  password    sufficient pam_unix.so 
Group1  empty string
Group2  empty string
Match2  password    sufficient pam_unix.so sha512 shadow use_authtok remember=2
Group1  remember=2
Group2  2

It seems that python is not supporting branch resets, is there any alternative?

EricStolz
  • 57
  • 1
  • 4
  • Not a duplicate but perhaps https://stackoverflow.com/q/5377782/14637 has some useful pointers. – Thomas Aug 29 '23 at 08:17
  • 2
    Just install the [PyPi regex module](https://pypi.org/project/regex/) (`pip install regex` in your console) and then use `import regex as re` and keep the current code as is. – Wiktor Stribiżew Aug 29 '23 at 08:17
  • Are you using `.findall()`? If yes, then [this](https://ideone.com/RAxPYa) would give your expected result, not? – InSync Aug 29 '23 at 08:46
  • ```import regex as re``` worked for me thanks – EricStolz Aug 29 '23 at 18:07

0 Answers0