Hi
I was hoping I could get a bit of help here. I am trying to break a number of strings up into arrays on particular words. For example in the following strings I want them broken on the words GET and/or POST:

"POST at the start without the other word"
"GET at the start without the other word"
"GET first\nthen POST and more"
"POST first\nthen GET and more"

so that I end off with:

["POST at the start without the other word"]
["GET at the start without the other word"]
["GET first\nthen ", "POST and more"]
["POST first\nthen ", "GET and more"]

Can anyone help?
A

Hi
I was hoping I could get a bit of help here. I am trying to break a number of strings up into arrays on particular words. For example in the following strings I want them broken on the words GET and/or POST:

"POST at the start without the other word"
"GET at the start without the other word"
"GET first\nthen POST and more"
"POST first\nthen GET and more"

so that I end off with:

["POST at the start without the other word"]
["GET at the start without the other word"]
["GET first\nthen ", "POST and more"]
["POST first\nthen ", "GET and more"]

Can anyone help?
A

Got it!

testStrings=["POST at the start without the other word", "GET at the start without the other word", "GET first\nthen POST and more", "POST first\nthen GET and more"]


for string in testStrings:
   newTmpArr=[]
   splitter = re.compile('(POST|GET)')
   tmpArr=splitter.split(string)
   tmpArr=[x for x in tmpArr if x != ""]
   str=""
   for x in tmpArr:
      if splitter.match(x):
         str=x
      else:
         str+=x
      newTmpArr.append(str)
   newTmpArr=[x for x in newTmpArr if x !="POST"]
   newTmpArr=[x for x in newTmpArr if x != "GET"]
   print newTmpArr

['POST at the start without the other word']
['GET at the start without the other word']
['GET first\nthen ', 'POST and more']
['POST first\nthen ', 'GET and more']

Its a bit ugly but it works. Anyone know of a nicer way to do this?

A

I think this one is better...

import re
testStrings=[ "POST at the start without the other word", "GET at the start without the other word", "GET first\nthen POST and more", "POST first\nthen GET and more"]
splitter = re.compile('(POST|GET)')
for s in testStrings:
    splittedString=splitter.split(s)[ 1:]
    print [ splittedString[i]+splittedString[ i+1] for i in range(0,len(splittedString),2)]

But you can even do

import re
teststrings=["POST at the start without the other word", "GET at the start without the other word", "GET first\nthen POST and more", "POST first\nthen GET and more"]
print [(lambda splittedstring: [splittedstring[i]+splittedstring[i+1] for i in range(0,len(splittedstring),2)])(re.compile('(POST|GET)').split(s)[1:]) for s in teststrings]

which is less readable.
You have a one liner if you don't use the intermediate variable teststrings ;-)

import re
print [(lambda splittedstring: [splittedstring[i]+splittedstring[i+1] for i in range(0,len(splittedstring),2)])(re.compile('(POST|GET)').split(s)[1:]) for s in ["POST at the start without the other word", "GET at the start without the other word", "GET first\nthen POST and more", "POST first\nthen GET and more"]]
commented: that is great. Thanks +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.