0

in perl you can do

if (/(.*)__(.*)/) {
   print "ans" . $1 . ' ' . $2 . "\n";
}

in python, can you do the same? It seems like you can only do

m = re.match(r"(.*)__(.*),data)
if m:
   print("ans {} {}".format(m[1],m[2]) 

Or is there better way to do this in more pythonic way?

Anyway, if this is the way to go, then it's gonna really suck if you have lot of big if and elif to test against many different possible patterns. I am really hoping someone who is more familiar w/ python can enlighten me

in perl

if (/(.*)__(.*)/){
   #do another
} elsif ( /(.*)XXX___(.*)/) {
   #do one thing
}
......

Imagine I have many test cases. In python, how would I do that?

user3502374
  • 781
  • 1
  • 4
  • 12
  • as far as I know, Python doesn't have global variables that automatically get set with regex matching groups, see also: https://stackoverflow.com/questions/2637592/perl-like-regex-in-python – Sundeep Jan 29 '19 at 08:53
  • 1
    regarding downvotes, that just seems to be the trend on `regex` tag – Sundeep Jan 29 '19 at 08:53
  • see also https://stackoverflow.com/questions/28311708/python-equivallent-of-perl-match-and-capture-within-if-block – Sundeep Jan 29 '19 at 08:55
  • 1
    much appreciate this post.. I was really hoping I could write this portion in python as I am learning and thought this was a great opportunity.. but this is bit too cumbersome.. will have to write this part in perl. thank you! – user3502374 Jan 29 '19 at 08:59

0 Answers0