New to python

Joined
Aug 7, 2023
Messages
3
Reaction score
1
Hello,
I am brand new to python. I have done this in Java.. but struggling to find the right regex in python. Can someone help please?

I need python code to replace " " with "\\ “, for the values coming after surName: and givenName:.


Some of the sample inputs are given below…


“surName: JOHN DOE JR || givenName: JAMES DOE SR”


“givenName: JAMES DOE SR || surName: JOHN DOE JR”


“surName: JOHN DOE JR && givenName: JAMES DOE SR”


“givenName: JAMES DOE SR && surName: JOHN DOE JR”


“surName: JOHN DOE || givenName: JAMES DOE”


“givenName: JAMES DOE SR || surName: JOHN DOE”


“surName: JOHN DOE && givenName: JAMES DOE SR”

Corresponding output should be..
“surName: JOHN\\ DOE\\ JR || givenName: JAMES\\ DOE\\ SR”


“givenName: JAMES\\ DOE\\ SR || surName: JOHN\\ DOE\\ JR”


“surName: JOHN\\ DOE\\ JR && givenName: JAMES\\ DOE\\ SR”


“givenName: JAMES\\ DOE\\ SR && surName: JOHN\\ DOE\\ JR”


“surName: JOHN\\ DOE || givenName: JAMES\\ DOE”


“givenName: JAMES\\ DOE\\ SR || surName: JOHN\\ DOE”


“surName: JOHN\\ DOE && givenName: JAMES\\ DOE\\ SR”
 
Joined
Dec 10, 2022
Messages
73
Reaction score
22
Is the input coming from a json file?

You can take a look at string replace



example may be

Python:
string = "surName:JOHN DOE JR || givenName:JAMES DOE SR"

string = string.replace(' ', '\\\\').replace('||','&&')
print(string)

Output
Code:
surName:JOHN\\DOE\\JR\\&&\\givenName:JAMES\\DOE\\SR
 
Last edited:
Joined
Aug 7, 2023
Messages
3
Reaction score
1
hello @menator01 thanks for the response..
The input is coming from a graphql input string..

but I do not want the space after JR or SR or before givenName replaced with \ ..
 
Joined
Dec 10, 2022
Messages
73
Reaction score
22
One way
Python:
string = "surName:JOHN DOE JR || givenName:JAMES DOE SR"

def replacer(arg):
    for i in range(2):
        arg = arg.replace(' ', '\\\\', 1)
        arg = arg[::-1].replace(' ', '\\\\', 1)
    return arg

print(replacer(string))

output
Code:
surName:JOHN\\DOE\\JR || givenName:JAMES\\DOE\\SR




Function I found with a search

Python:
def replacer(string, sub, wanted, n):
    where = [m.start() for m in re.finditer(sub, string)][n-1]
    before = string[:where]
    after = string[where:]
    after = after.replace(sub, wanted, 1)
    newstring = before + after
    return newstring

for i in range(2):
    string = replacer(string, ' ', '\\\\', 1)
    string = replacer(string[::-1], ' ', '\\\\', 1)

print(string)

output
Code:
surName:JOHN\\DOE\\JR || givenName:JAMES\\DOE\\SR

The link
https://stackoverflow.com/questions/35091557/replace-nth-occurrence-of-substring-in-string
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,794
Messages
2,569,641
Members
45,355
Latest member
SJLChristi

Latest Threads

Top