given an input ( http parameters ) with markers ( * ) to the tool, I would like to replace the markers with a string, but just one mark at time
the code:
....
inp = input("http params:" ) # aa=bb*&cc=dd*&ee=ff*
attributes = ["AA","BB","CC"]
mark = "*"
for mark in inp:
for attribute in attributes:
data = inp.replace("*", ")(" attribute)
print(data)
....
I have this output:
aa=bb)(AA&cc=dd)(AA&ee=ff)(AA
aa=bb)(BB&cc=dd)(BB&ee=ff)(BB
aa=bb)(CC&cc=dd)(CC&ee=ff)(CC
[..]
while I would like to have this:
aa=bb)(AA&cc=dd&ee=ff
aa=bb)(BB&cc=dd&ee=ff
aa=bb)(CC&cc=dd&ee=ff
aa=bb&cc=dd)(AA&ee=ff
aa=bb&cc=dd)(BB&ee=ff
aa=bb&cc=dd)(CC&ee=ff
aa=bb&cc=dd&ee=ff)(AA
aa=bb&cc=dd&ee=ff)(BB
aa=bb&cc=dd&ee=ff)(CC
CodePudding user response:
Here, we will gonna need some function to replace on specific number of *
instead of replace on all. Moreover, I have reference the following nth_repl
fun from here.
Code:
def nth_repl(s, sub, repl, n):
find = s.find(sub)
# If find is not -1 we have found at least one match for the substring
i = find != -1
# loop util we find the nth or we find no match
while find != -1 and i != n:
# find 1 means we start searching from after the last match
find = s.find(sub, find 1)
i = 1
# If i is equal to n we found nth match so replace
if i == n:
return s[:find] repl s[find len(sub):]
return s
inp = input("http params:" ) # aa=bb*&cc=dd*&ee=ff*
attributes = ["AA","BB","CC"]
for n in range(1, inp.count('*') 1):
for attribute in attributes:
data = nth_repl(inp, "*", f')({attribute}',n)
print(data)
Or without func:
for i, v in enumerate(inp):
for attribute in attributes:
if '*' in v:
print(inp[:i] f')({attribute}' inp[i 1:])
Output:
http params:aa=bb*&cc=dd*&ee=ff*
aa=bb)(AA&cc=dd*&ee=ff*
aa=bb)(BB&cc=dd*&ee=ff*
aa=bb)(CC&cc=dd*&ee=ff*
aa=bb*&cc=dd)(AA&ee=ff*
aa=bb*&cc=dd)(BB&ee=ff*
aa=bb*&cc=dd)(CC&ee=ff*
aa=bb*&cc=dd*&ee=ff)(AA
aa=bb*&cc=dd*&ee=ff)(BB
aa=bb*&cc=dd*&ee=ff)(CC