How can I convert a Perl regex with named groups to Python? -


I am trying to change the following Pearl regex which is found in the Perl module, a Python 2.5.4 regex To parse a filename

  #parl> V5.10 re = & gt; '^ (? (& Lt; name & gt; *) [\ / s ._-] *.?) (& Lt; openb & gt; \ [?] (& Lt; weather & gt; \ d { 1,2} ?? (? & Lt; episode & gt; \ d {1,2}) (: - (: \ k & lt; weather & gt; x)) [x \ /]? (? & LT; endep & gt; \ d {1,2}) (? (? Lt; openb & gt;)]] (?: [\ S ._-] * (? & Lt; epname & gt; [^ I would like to use named groups as well, and I know that regex extensions are different for named groups in Python, but I have syntax I am not 100% sure.  

This is what I have tried:

  # Python (not working) r = re.compile (r '^ ( ? :(? P & lt; name & Gt;.?) [\ / \ S ._-] *)? (? P & LT; openb & gt; \ [] (P & LT;; weather & gt; \ d {1,2})? [ X \ /] (P & LT;; Case & gt; \ d {1,2})? (:-(?: KP & LT; Weather & gt; X) (P & LT; endep & gt; D {1,2}) ((P & LT; openb & gt;) \]) (??? [\ s ._-] * (P & LT ;? EPName & gt; [^ \ /] + I get the error:  
  increase error, v # invalid expression sre_constants error: bad character in group name  

/ Code>

For example, this one I managed to convert and it works. But above the one I can not seem to get right I get a compilation error in Python.

  #Parl: re = gt; '^ (?? (& Lt; name & gt; *) [\ / \ r ._-])? (?: | Se | weather | series) [\ s ._-] ( ?? \ \ {d2}} [x \ / \ r ._-] * (?: E | EP | Episode | [\ / \ s ._-]) [\ s ._-] (? From & lt; episode & gt; \ d {1,2}) (: - ?? (? :( ?: E | EP)? [\ S ._] *) ( \ d {1,2})) (??????? [\ s ._] (?: P | [Part] [\ s ._] (lt and; part & gt; \ d +)) (& lt; subep & gt; [az]) (?: [\ / \ S ._-] * (? & [Ep  / \ S ._-]) ([: | se | weather | series] [\ s ._-] (p. & Lt ;? weather & gt; \ d {1?, 2}) [x \ / \ R ._-] * (?: E | EP | Episode | [\ / .rr ._-]) [\ s ._-] (P & LT;? Case & gt; \ {{{{1}} ) : - | [\ s ._] *) (P & LT; endep & gt; \ d {1,2})) (?: ((?:??: EEP ??) ?? ?? .?] ([: P | part] [\ s ._] (p & lt; part & gt; \ d +?)) (P & lt; subep & gt; [az]) ([\ /? I do not believe where I am Have to show   

There are 2 problems with your translation. First, the second mention of openb is the additional bracket around it, it is not an expression of a name.

The next is that you \ Kashmir , using (P = Season) to use Python, the following compilation for me:

  r = re.compile (r ' ^ (?:(? P & lt; name & gt ;.?) [\ / \ S ._-] *)? (? P & lt; openb & gt; \ [] (p & lt ;? weather & Gt; \ d {1,2})? [X \] (P & LT;; Episode & gt; \ {{{}}? (:-(?: (? P = season) x) (P & LT; endep & gt; \ d {1,2})) ((openb)]] (? [\ S ._-] * (P & LT; epname & gt; ??? [^ \ /] +?))? $ ')  

If I were you, I would reuse this expression for several lines and add rich documentation So that you can understand the expression in the future, though it is something that needs to be maintained continuously.

(The second OpenB / <> code was edited after a conditional expression was translated, and translated properly backbreaking.)


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -