1

I am trying to parse a comma separated string keyword://pass@ip:port. The string is a comma separated string, however the password can contain any character including comma. hence I can not use a split operation based on comma as delimiter.

I have tried to use regex to get the string after "myserver://" and later on I can split the rest of the information by using string operation (pass@ip:port/key1) but I could not make it working as I can not fetch the information after the above keyword.

myserver:// is a hardcoded string, and I need to get whatever follows each myserver as a comma separated list (i.e. pass@ip:port/key1, pass2@ip2:port2/key2, etc)

This is the closest I can get:

import re  
my_servers="myserver://password,123@ip:port/key1,myserver://pass2@ip2:port2/key2"
result = re.search(r'myserver:\/\/(.*)[,(.*)|\s]', my_servers)

using search I tries to find the occurrence of the "myserver://" keyword followed by any characters, and ends with comma (means it will be followed by myserver://zzz,myserver://qqq) or space (incase of single myserver:// element, but I do not know how to do this better apart of using space as end-indicator). However this does not come out right. How can I do this better with regex?

4
  • Is myserver a hardcoded string? Do you need to get [ "password,123@ip:port/key1", "pass2@ip2:port2/key2" ]? Commented May 9, 2017 at 12:33
  • yes imyserver is a hardcoded string and I need to get exactly "password,123@ip:port/key1", and "pass2@ip2:port2/key2" (for server comma separated string, or "pass@ip:port/key" if it is only contain information in the string) @Wiktor Stribiżew Commented May 9, 2017 at 12:37
  • 1
    See ideone.com/JEBXVX - like this? 1) [['password', '123@ip:port/key1'], ['pass2@ip2:port2/key2']] or 2) ['password,123@ip:port/key1', 'pass2@ip2:port2/key2']? Sorry, you are being too verbose. Your input has no pass@ip:port/key1 substring. Commented May 9, 2017 at 12:37
  • @Wiktor Stribiżew the information is password@ip:port/key . hence "password,123" will be a password that contains comma. Maybe you can add your answer not as comment so I can upvote this and continue the discussion ? Commented May 9, 2017 at 12:40

1 Answer 1

3

You may consider the following splitting approach if you do not need to keep myserver:// in the results:

filter(None, re.split(r'\s*,?\s*myserver://', s))

The \s*,?\s*myserver:// pattern matches an optional , enclosed with 0+ whitespaces and then myserver:// substring. See this regex demo. Note we need to remove empty entries to get rid of an empty leading entry as when the match is found at the string start, the empty string at the beginning will be added to the resulting list.

Alternatively, you can use the lookahead based pattern with a lazy dot matching pattern with re.findall:

rx = r"myserver://(.*?)(?=\s*,\s*myserver://|$)"

See the Python demo

Details:

  • myserver:// - a literal substring
  • (.*?) - Capturing group 1 whose contents will be returned by re.findall matching any 0+ chars other than line break chars, as few as possible, up to the first occurrence (but excluding it)
  • (?=\s*,\s*myserver://|$) - either of the 2 alternatives:
    • \s*,\s*myserver:// - , enclosed with 0+ whitespaces and then a literal myserver:// substring
    • | - or
    • $ - end of string.

Here is the regex demo.

See a Python demo for the both approaches:

import re

s = "myserver://password,123@ip:port/key1,myserver://pass2@ip2:port2/key2"

rx1 = r'\s*,?\s*myserver://'
res1 = filter(None, re.split(rx1, s))
print(res1)

#or
rx2 = r"myserver://(.*?)(?=\s*,\s*myserver://|$)"
res2 = re.findall(rx2, s)
print(res2)

Both will print ['password,123@ip:port/key1', 'pass2@ip2:port2/key2'].

Sign up to request clarification or add additional context in comments.

1 Comment

Python demo is nice, but it would be nice if you will put the demo and its results in the post.

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.