I have a string that looks as follows:
word1||word2||word3||word4
What is the best way to remove the 'extra' | between words in the string without getting rid of both of them?
The end product needs to look like:
word1|word2|word3|word4
You can use replace
str='word1||word2||word3||word4'
print(str.replace('||', '|'))
#word1|word2|word3|word4
You can use a regex that matches one or more occurrences of the pattern:
import re
s='word1||word2||word3||word4'
re.sub('\|+','|',s)
# 'word1|word2|word3|word4'
Simply use str.replace :
('word1||word2||word3||word4').replace ('||', '|')
below code should address your requirement.
str1="word1||word2||word3||word4|word6"
str1 = str1.replace("||","|")
print (str1)
You can replace || with | using replace functions.
str='word1||word2||word3||word4'
print(str.replace('||', '|'))
Thanks. :)
Related
I have some URLs and I need some of them to be stripped from the question mark (?)
Ex. https://www.yelp.com/biz/starbucks-san-leandro-4?large_photo=1
I need it to return https://www.yelp.com/biz/starbucks-san-leandro-4
How can I do that?
you can also use .split() method
The split() method splits a string into a list.
You can specify the separator, default separator is any whitespace.
Syntax
string.split(separator, maxsplit)
data = 'https://www.yelp.com/biz/starbucks-san-leandro-4?large_photo=1'
print (data.split('?')[0])
output:
https://www.yelp.com/biz/starbucks-san-leandro-4
You could use rfind and slice the string up to the returned index:
s = 'https://www.yelp.com/biz/starbucks-san-leandro-4?large_photo=1'
s[:s.rfind('?')]
# 'https://www.yelp.com/biz/starbucks-san-leandro-4'
Go for a regular expression
import re
new_string = re.sub(r'\?.+$', '', your_string)
See a demo on regex101.com.
I would parse the url and the rebuild it with the parts that you want to keep. For example you can use urllib.parse
How can i get word example from such string:
str = "http://test-example:123/wd/hub"
I write something like that
print(str[10:str.rfind(':')])
but it doesn't work right, if string will be like
"http://tests-example:123/wd/hub"
You can use this regex to capture the value preceded by - and followed by : using lookarounds
(?<=-).+(?=:)
Regex Demo
Python code,
import re
str = "http://test-example:123/wd/hub"
print(re.search(r'(?<=-).+(?=:)', str).group())
Outputs,
example
Non-regex way to get the same is using these two splits,
str = "http://test-example:123/wd/hub"
print(str.split(':')[1].split('-')[1])
Prints,
example
You can use following non-regex because you know example is a 7 letter word:
s.split('-')[1][:7]
For any arbitrary word, that would change to:
s.split('-')[1].split(':')[0]
many ways
using splitting:
example_str = str.split('-')[-1].split(':')[0]
This is fragile, and could break if there are more hyphens or colons in the string.
using regex:
import re
pattern = re.compile(r'-(.*):')
example_str = pattern.search(str).group(1)
This still expects a particular format, but is more easily adaptable (if you know how to write regexes).
I am not sure why do you want to get a particular word from a string. I guess you wanted to see if this word is available in given string.
if that is the case, below code can be used.
import re
str1 = "http://tests-example:123/wd/hub"
matched = re.findall('example',str1)
Split on the -, and then on :
s = "http://test-example:123/wd/hub"
print(s.split('-')[1].split(':')[0])
#example
using re
import re
text = "http://test-example:123/wd/hub"
m = re.search('(?<=-).+(?=:)', text)
if m:
print(m.group())
Python strings has built-in function find:
a="http://test-example:123/wd/hub"
b="http://test-exaaaample:123/wd/hub"
print(a.find('example'))
print(b.find('example'))
will return:
12
-1
It is the index of found substring. If it equals to -1, the substring is not found in string. You can also use in keyword:
'example' in 'http://test-example:123/wd/hub'
True
Hi and thank you for your time.
I have the following example string: "Hola Luis," but the string template will always be "Hola {{name}},".
How would the regex be to match any name? You can assume the name will follow a blank space and "Hola" before that and it will have a comma right after it.
Thank you!
You can use the following regular expression, assuming that as you mention, the format is always the same:
import re
s = "Hola Luis,"
re.search('Hola (\w+),', s).group(1)
# 'Luis'
s = 'Hola test'
re.match(r'Hola (\w+)', s).groups()[0]
results:
'test'
Continuing from #yatu,
Without regex:
print("Hola Luis,".split(" ")[1].strip(","))
Explanation:
split(" ") # to split the string with spaces
[1] # to get the forthcoming part
strip(",") # to strip off any ','
OUTPUT:
Luis
According to Falsehoods Programmers Believe About Names and your requirements, I'll use the following regex: (?<=Hola )[^,]+(?=,).
I need some help with a regular expression in python.
I have a string like this:
>>> s = '[i1]scale=-2:givenHeight_1[o1];'
How can I remove givenHeight_1 and turn the string to this?
>>> '[i1]scale=-2:360[o1];'
Is there an efficient one-liner regex for such a job?
UPDATE 1:
my regex so far is something like this but currently not working:
re.sub('givenHeight_1[o1]', '360[o1]', s)
You can use positive look around with re.sub :
>>> s = '[i1]scale=-2:givenHeight_1[o1];'
>>> re.sub(r'(?<=:).*(?=\[)','360',s)
'[i1]scale=-2:360[o1];'
The preceding regex will replace any thing that came after : and before [ with an '360'.
Or based on your need you can use str.replace directly :
>>> s.replace('givenHeight_1','360')
'[i1]scale=-2:360[o1];'
I have string looking like this:
'Toy Story..(II) (1995)'
I want to split the line into two parts like this:
['Toy Story..(II)','1995']
How can I do it? Thanks.
This code will get you started:
'Toy Stroy..(II) (1995)'.rstrip(')').rsplit('(',1)
Other than that, you can use r'\s*[(]\d{4}[)]\s*$' to match a four-digit number in parentheses at the end of the string. If you find it, you can chop it off:
s = ''
l = [s]
match = re.compile(r'\s*[(]\d+[)]\s*$').search(s)
if match is not None:
l = [s[:len(match.group(0))], s[-len(match.group(0)):].trim]
One way is this:
s = 'Toy Stroy..(II) (1995)'
print s[:s.rfind('(')].strip()
print s[s.rfind('('):].strip("()")
Output:
Toy Stroy..(II)
1995
>>>
You could use regular expressions for that. See here: http://www.amk.ca/python/howto/regex/
Or you could use the split function and then manyally remove the parenthesis or other non desired characters. See here: http://www.python.org/doc/2.3/lib/module-string.html
l[:-1].split("(")