Python: split on two different expressions? [duplicate] - python

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Python strings split with multiple separators
Is there a way in Python to split a string on two different keys?
Say I have a string like this:
mystr = "wketjwlektjwltjkw<br/>wwwweltjwetlkwjww" + \
"wwetlwjtwlet<strong>wwwwketjwlektjwlk</strong"
I'd like to split on EITHER <br/>wwww or <strong>wwww.
How should I do this?
Thanks!

import re
re.split(r'<(br\/|strong)>wwww', mystr)[::2]

Related

String replace() vs string translate() method [duplicate]

This question already has answers here:
How to replace two things at once in a string?
(6 answers)
using .replace to replace more than one character in python [duplicate]
(4 answers)
Closed 2 years ago.
I have a question regarding my code below:
Input: A DNA string Pattern (ex: 'AAAACCCGGT')
Output: The complementary string (ex: 'TTTTGGGCCA')
def Complement(Pattern):
comPattern=Pattern.translate(str.maketrans({'A':'T','T':'A','G':'C','C':'G'}))
return comPattern
I tried using str.replace() method multiple times for above problem, but it did not work. Any idea why?

Make a list with multiple possible strings from file names with regex [duplicate]

This question already has answers here:
Regex match one of two words
(2 answers)
Closed 3 years ago.
I want to make a list of several PNG in a folder based on multiple references. So in the list I want the PNG that have the string "7029113" OR "7031503" in their name. This is what I got so far, I only need to know how to do OR with regex, and probably my wildcards are wrong too I'm not sure.
render_path = "C:/BatchRender/Renaming"
os.chdir(render_path)
list_files = glob.glob("*.png")
r = re.compile(".*7029113.*" OR ".*7031503.*")
list_40 = list(filter(r.match, list_files))
This is one way of doing it.
r = re.compile('.*(7029113|7031503).*')

Is there a better way to do booleans? [duplicate]

This question already has answers here:
Check if multiple strings exist in another string
(17 answers)
Closed 3 years ago.
if "a_string" in random_word or "b_string" in random_word:
...
Is there a cleaner, less dense way to write this boolean?
In case of if, you could use any to check for the occurrence of either of the two strings
if any(i in random_word for i in ["a_string", "b_string"]):
You can use a regular expression.
import re
if re.search(r'a_string|b_string', random_word):
...

Split string when ";" appears [duplicate]

This question already has answers here:
How can I split and parse a string in Python? [duplicate]
(3 answers)
Closed 5 years ago.
I need to split the string everytime ; shows up.
words = "LightOn;LightOff;LightStatus;LightClientHello;"
Output should be something like this:
LightOn
LightOff
LightStatus
LightClientHello
Simply, everytime it finds ; in a string, it has to split it.
Thank you for help
res = words.split(";")
Refer to this link for more information on split.

regular expression in python: best way to change "a-b-cde" into ['a', 'b', 'cde'] [duplicate]

This question already has answers here:
How do I split a string into a list of words?
(9 answers)
Closed 6 years ago.
I have an index where words are split in syllabes in the form 'foo-bar' and I want to create the list ['foo', 'bar'].
I am new to python, and especially to the re module. So do I ask for the best way to do this instead of writing ugly code.
There is no need of using any module. Use the inbuilt string split method.
a = "a-b-cde"
b = a.split('-')
#b contains ['a','b','cde']

Categories