0

I have a variable like this:

bug = "^bug:url1.com;url2.com;url3.com;url4.com^"

And I want the end result to be like this:

bug = ["url1.com","url2.com","url3.com","url4.com"]

I tried:

#!/usr/bin/python

bug = "^bug:url1.com;url2.com;url3.com;url4.com^"
bug = bug.split(";")
print bug

But it outputs:

['^bug:url1.com', 'url2.com', 'url3.com', 'url4.com^']

Please note that the variable bug consists of a bunch of URLs not just ordinary words, maybe with regex ? I don't know sorry I'm still new to programming, please help me fix this.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Squidward
  • 131
  • 3
  • 14

5 Answers5

2

I think the existing answers are too complicated for this simple task so I'm posting my comment as an answer:

>>> bug = "^bug:url1.com;url2.com;url3.com;url4.com^"
>>> bug[5:-1].split(';')
['url1.com', 'url2.com', 'url3.com', 'url4.com']

You slice the unwanted characters from the beginning and end of your string and afterwards you split the string by your delimiter ;. Of course, if there's anything dynamic about the format of your string, e.g. it could start with '^someunwantedtext:', then use a regular expression.

timgeb
  • 76,762
  • 20
  • 123
  • 145
  • Problem is "*the variable bug consists of a bunch of URLs not just ordinary words*" so `bug` might not be always of the same size. Your solution will only work if you know the length of URLs which if I understand well is not the case here. – Nuageux May 12 '17 at 13:59
  • @Nuageux no, this solution is independent from the length of the urls. You discard the first 5 and the last character from the string. What's in between does not matter. – timgeb May 12 '17 at 14:00
  • My mistake you are right. I misunderstood some part of the question. I thought that "bug" could be something else that just "bug". I'm not a native english speaker, sorry. – Nuageux May 12 '17 at 14:06
0

First, split to remove part before ':' and to remove part after '^'. Then split for each ';'

bug = "^bug:url1.com;url2.com;url3.com;url4.com^"
bug = bug.split(":")[1:][0].split("^")[:-1][0]
bug = bug.split(";")
print bug
# ['url1.com', 'url2.com', 'url3.com', 'url4.com']
Nuageux
  • 1,668
  • 1
  • 17
  • 29
0

You could use lstrip() and rstrip(). This way you could even have ^ and bug inside the url and it won't be stripped.

bug = "^bug:url1.com;url2.com;url3.com;url4.com^"
buglist = bug.lstrip("^bug").lstrip(":").rstrip("^").split(";")

Output: ['url1.com', 'url2.com', 'url3.com', 'url4.com']

Nuageux
  • 1,668
  • 1
  • 17
  • 29
L.S.
  • 476
  • 4
  • 10
0

You can actually use regex for this purpose! replace the bug and special character in your data and split the urls with ;

import re
bug = "^hi.com;hi.com:url1.com;url2.com;url3.com;url4.com^"
print re.sub(r'((\w+.com;?)*:)|\^','',bug).split(';')

Output:

['url1.com', 'url2.com', 'url3.com', 'url4.com']
Keerthana Prabhakaran
  • 3,766
  • 1
  • 13
  • 23
0

A combination of replace and split does the job:

>>> s = "^bug:url1.com;url2.com;url3.com;url4.com^"
>>> s.replace('^','').replace('bug:','').split(';')
['url1.com', 'url2.com', 'url3.com', 'url4.com']

Step-by-step explanation

>>> s.replace('^','')
'bug:url1.com;url2.com;url3.com;url4.com'
>>> s.replace('^','').replace('bug:','')
'url1.com;url2.com;url3.com;url4.com'
>>> s.replace('^','').replace('bug:','').split(';')
['url1.com', 'url2.com', 'url3.com', 'url4.com']
>>>

A better solution

As timgeb mentioned my method fails if the urls contain the string "bug:". timgeb's solution (https://stackoverflow.com/a/43939538/2194843) seems to be fine:

>>> s[5:-1].split(';')
['url1.com', 'url2.com', 'url3.com', 'url4.com']
Community
  • 1
  • 1
funk
  • 2,221
  • 1
  • 24
  • 23