0

I would like to use a regular express in Python that is written in Perl as:

$text =~ s/\[\[category:([^|\]]*)[^]]*\]\]/[[$1]]/ig;

Anyone has an idea about how to do that? I will also need to extract the capture group ($1 in Perl).

mob
  • 117,087
  • 18
  • 149
  • 283
riverain
  • 13
  • 1
  • 1
    Use `\1` for backreferencing in python. `re.sub` replace `s///` see this: http://stackoverflow.com/questions/5984633/python-re-sub-group-number-after-number – hmatt1 Apr 02 '14 at 02:35
  • 4
    What do you mean by "implement"? Do you want to implement a regex engine in Python, or are you merely looking to find the Python regex that is equivalent to the Perl regex you gave? Have you looked at the Python online documentation? It's complete and with just a little studying you should be able to figure it out. – Jim Garrison Apr 02 '14 at 03:03

1 Answers1

0

In python you use re.sub instead of perl s/// operator. You give it regular expression string as a first argument. A special syntax for "raw strings" which don't need \ escaped, like r'\', will help you. A substitution comes as a second argument, you also use \1 instead of $1 for backref. There is no such thing as topic variable in python, so you need to explicitly pass source string as a third argument. Then you add regular expression flags as dedicated named argument, their values are stored in re.I, re.S, ... constants. You don't need g flag as re.sub is global by default:

import re
text = re.sub(r'\[\[category:([^|\]]*)[^]]*\]\]', '[[\1]]', 
              text, flags=re.I)
Suor
  • 2,845
  • 1
  • 22
  • 28