Regular expression help

This is the place for queries that don't fit in any of the other categories.

Regular expression help

Postby CoximusPrime » Sat Nov 23, 2013 5:18 pm

Hi Guys, hopefully this will be a quick one for some lucky person smarter than me. I'm essentially using the following :

Code: Select all
import re

def testfunction(found):
   myarray = {
         "%!USERNAME!%" : "USER",
         "%!PASSWORD!%" : "PASS"
         }
   if found in myarray:
      return myarray[found]
   else:
      return found

inpurtStr = "text to perform regex upon %!USERNAME!%"
outputStr = re.sub(r'%!(.*)!%', lambda m: testfunction(m.group()), inpurtStr)
print outputStr


this finds %!USERNAME!% in the input string and replaces it with "USER" from the array ... however ...... If I than change the inputStr to :

Code: Select all
"text to perfome regex upon %!USERNAME!% %!PASSWORD!%"


I was hoping that the function would be called multiple times, once per found match. Instead it calls the function once and passes in a single string of
Code: Select all
"%!USERNAME!% %!PASSWORD!%"


Can anyone tell me if what I'm hoping to achieve is even possible, and possibly suggest any solutions?

Cheers
Last edited by stranac on Sat Nov 23, 2013 5:34 pm, edited 1 time in total.
Reason: First post lock.
CoximusPrime
 
Posts: 2
Joined: Sat Nov 23, 2013 5:06 pm

Re: Regular expression help

Postby stranac » Sat Nov 23, 2013 5:43 pm

Well, * is greedy by default(meaning it will match as many characters as possible), so r'%!(.*)!% matches "%!USERNAME!% %!PASSWORD!%"
You can make it non-greedy by using *? instead.

This:
Code: Select all
   if found in myarray:
      return myarray[found]
   else:
      return found

is equivalent to:
Code: Select all
    return myarray.get(found, found)


Also, I would make testfunction() take a match object, which would get rid of that ugly lambda.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1092
Joined: Thu Feb 07, 2013 3:42 pm

Re: Regular expression help

Postby CoximusPrime » Sat Nov 23, 2013 6:14 pm

Many thanks for that, it works a treat!! ... My lack of regex showing me up there (and general novice status with python). For reference my complete code for this now is :

Code: Select all
def testfunction(match):
   myarray = {
         "%!USERNAME!%" : "USER",
         "%!PASSWORD!%" : "PASS"
         }
   return myarray.get(match.group(), match.group())

inputStr = "PONG %!USERNAME!% %!PASSWORD!%"
outputStr = re.sub(r'%!(.*?)!%', testfunction, inputStr)
print outputStr


The aim is to use this to build an IRC client, which will have an associative array with a key which references and command from a server, and a response message such as :

Code: Select all
"PONG %!USERNAME!% %!PASSWORD!%"


which I can then use to replace specific elements of the string ... so if the server sent a "PING" command, I would have an array containing :
Code: Select all
commandArray = {"PING": "PONG %!USERNAME!%}


So using the inbound command as a key in this array, I can simply lookup the relevent response, and replace some elements with user specific credentials, and then send this to the server ... I've got the socket side of things working, this was my last remaining blocker, so hopefully I can get this finished now.

Thanks again
CoximusPrime
 
Posts: 2
Joined: Sat Nov 23, 2013 5:06 pm


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 2 guests