0

I am using a REST library whose development team only supports PHP. Pretty much everything are finished except translating this part of their PHP code to my Python codebase. the "preg_match" is tough for me, please help.

Their PHP:

function strippadding($string) {
 $slast = ord(substr($string, -1));
 $slastc = chr($slast);
 $pcheck = substr($string, -$slast);
 if (preg_match("/$slastc{" . $slast . "}/", $string)) {
 $string = substr($string, 0, strlen($string) - $slast);
 return $string;
 } else {
 return false;
 }
}

My Python:

def strip_padding_pay2go(string):
 last_string = string[-1]
 slast = ord(last_string)
 slastc = chr(slast)
 pcheck = last_string[:-slast]
 if re.search("/" + str(slastc) + "{" + str(slast) + "}/", string):
 last_char = len(string) - slast
 new_string = string[0: last_char]
 return new_string
 else:
 return False

But i keep failing ( returns False ).

asked May 26, 2017 at 4:50
3
  • You're looking for python's re module. Take a look at re.findall and re.search. Commented Jun 8, 2017 at 7:50
  • 1
    i updated the question Commented Jun 8, 2017 at 7:58
  • 1
    Python regexen don't use delimiters (starting and ending /)... Commented Jun 8, 2017 at 8:01

1 Answer 1

1

I got it

def strip_padding_pay2go(string):
 last_string = string[-1]
 slast = ord(last_string)
 slastc = chr(slast)
 pcheck = last_string[:-slast]
 if re.search(str(slastc) + "{" + str(slast) + "}", string):
 last_char = len(string) - slast
 new_string = string[0: last_char]
 return new_string
 else:
 return False
answered Jun 8, 2017 at 8:02
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.