regex split to split a string [duplicate] - python

This question already has answers here:
What is a non-capturing group in regular expressions?
(18 answers)
the output of re.split in python doesn't make sense to me
(3 answers)
Closed 2 years ago.
I need to split "WUBWUBIWUBAMWUBWUBX" such that I get the words ['I','AM','X']
I tried using this line:
l=re.split("([W][U][B])+","WUBWUBIWUBAMWUBWUBX")
I got the output like :
['', 'WUB', 'I', 'WUB', 'AM', 'WUB', 'X']

Related

Split string by separators [duplicate]

This question already has answers here:
Split string based on regex
(3 answers)
Closed 2 years ago.
I'm try to split text from string, but I can't.
I have 1;2 | 2;4, for example in my string, and I want to:
['1;2', '|', '2;4']
You can use str.partition:
>>> '1;2 | 2;4'.partition("|")
('1;2 ', '|', ' 2;4')

converting a string to a readable sentence [duplicate]

This question already has answers here:
How do I convert a list into a string with spaces in Python?
(6 answers)
Closed 4 years ago.
say you have a list like so:
lst = ['my', 'name', 'is', 'jack.']
If I convert to a string doing this:
''.join(lst)
output
'mynameisjack.'
How do I make the list print out:
"my name is jack."
instead of all together.
Instead of using ''.join(lst)(an empty string), use ' '.join(lst), with a space (see the documentation of join!).

Removing non-alphabetical characters from a list element in python [duplicate]

This question already has answers here:
Python, remove all non-alphabet chars from string
(7 answers)
Stripping everything but alphanumeric chars from a string in Python
(16 answers)
Get the first character of the first string in a list?
(5 answers)
Closed 5 years ago.
Given a list
['R2', 'Y1-1', 'Y1-2', 'Y2-1', 'Y2-2', 'r']
i wish to remove all non-alphabetical characters and return the list in the form
['R', 'Y', 'Y', 'Y', 'Y', 'r']
Is there a way of doing this in python 3.6.3?

Splitting on a lookahead [duplicate]

This question already has answers here:
Decode HTML entities in Python string?
(6 answers)
Closed 6 years ago.
I'm trying to split on a lookahead, but it doesn't work for the last occurrence. How do I do this?
my_str = 'HRC’s'
import re
print(re.split(r'.(?=&)', my_str))
My output:
['HR', '&#226', '&#128', '™s']
My desired output:
['HRC', '&#226', '&#128', '&#153', 's']
The solution using re.findall() function:
my_str = 'HRC’s'
result = re.findall(r'\w+|&#\d+(?=;)', my_str)
print(result)
The output:
['HRC', '&#226', '&#128', '&#153', 's']

How can I split a pipe-separated string into a list? [duplicate]

This question already has answers here:
Split a string by a delimiter in python
(5 answers)
Closed 3 years ago.
I have this string:
var(HELLO,)|var(Hello again)| var(HOW ARE YOU?)|outV(0)|outV(1)|outV(2)|END
I want to split it on the |. I don't want it to split at the white space, only at the |.
Is this possible?
The way to do this is clearly documented here.
Example:
>>> myString = "subString1|substring2|subString3"
>>> myString = myString.split("|")
>>> print myString
["subString1", "subString2", "subString3"]

Categories