0

I'm trying to split the string in to two strings

 INPUT: "ASSO|ASSOCS|AS|ASSOCIATES/ASSOC/"

OUTPUT: "ASSO|ASSOCS|AS|ASSOCIATES","ASSOC"

Tried : I tried removing the the last character "/" first and replaced the other with ",". Wanted to know can we do this both steps in once using regex

str=str.replace(/\/$/, "")
str=str.replace(\/,",")
4
  • 3
    You can simply use filter(None, str.split('/')) Commented May 2, 2018 at 13:11
  • print(",".join(filter(None, INPUT.split('/')))) Commented May 2, 2018 at 13:15
  • 1
    Or re.findall(r"[^/]+", s) Commented May 2, 2018 at 13:17
  • are you perhaps looking for re.finditer("(.+?)/", INPUT)? Commented May 2, 2018 at 13:20

1 Answer 1

1

If you are "trying to split the string in to two strings", then you could do

s = "ASSO|ASSOCS|AS|ASSOCIATES/ASSOC/"
s1, s2, _ = s.split('/')
Sign up to request clarification or add additional context in comments.

1 Comment

This is generally a better option than regex. I would personally assign the output to a list however rather than using argument unpacking. If the list returned from string.split() is too short, you'll get an error.

Your Answer

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.