0

I have following Perl regular expression that replaces number with results of evaled()ed Perl snippet:

$label =~ s/(\d+)999999(\D|\Z)/($1 +1) ."M$2"/xeg;
$label =~ s/(\d+)999(\D|\Z)/($1 +1) ."k$2"/xeg;

I have reviewed Python 2 and 3 documentation https://docs.python.org/3/howto/regex.html on Regular expressions, but can't find an equivalent in Python.

Any ideas?

In above examples, it cuts numbers that end with "999999" to suffix "M", and similarly for "999" to suffix "k". For example, number 1999999 becomes "2M" after replacement. Although this is not a how-to question for this specific example, but a more generic question if it's possible to have an eval()ed Python code as part of replacement generation?

Tagar
  • 13,911
  • 6
  • 95
  • 110
  • you will have to do it more verbosely in python. – Jason Hu Nov 06 '17 at 21:39
  • Could you describe what you want to do for those of us who speak python but don't read perl? – Aran-Fey Nov 06 '17 at 21:39
  • It cuts numbers that end with "999999" to suffix "M", and similarly for "999" to suffix "k". For example, number 1999999 becomes "2M" after replacement. – Tagar Nov 06 '17 at 21:41
  • Can you provide us with examples of what it SHOULD and SHOULD NOT match? – ctwheels Nov 06 '17 at 21:42
  • 1
    Your question should actually contain the Python code that does not work. SO is not a free code converting service. – Wiktor Stribiżew Nov 06 '17 at 21:45
  • @Wiktor Stribiżew I am not asking to convert that code. It's a question if Python has an equivalent for that functionality. I can convert to Python myself once I know if it's possible to achieve this in Python. – Tagar Nov 06 '17 at 21:46
  • Certainly it has, and that has been asked thousands of times. – Wiktor Stribiżew Nov 06 '17 at 21:46
  • @WiktorStribiżew, that question you pointed to is not a "dupe". I know how to use groups in Python `re` module. This questions about how to substitute to an eval()ed python code? It's very different and distinct from that question. – Tagar Nov 06 '17 at 21:49
  • Pass the callback method as the replacement argument, that is it. Or use a lambda expression. – Wiktor Stribiżew Nov 06 '17 at 21:50

0 Answers0