Split string by both newline and full stop? [duplicate] - python

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
If I have a string like test string. something \n else how would I split the string on both . and \n characters? I have tried using re.split(". \n", text) but it doesn't seem to be working.

Figured it out - I needed to use re.split("\\.|\n", text).

Related

Python Regex to match a colon either side (left and right) of a word [duplicate]

This question already has answers here:
Regex to find and replace emoji names within colons
(4 answers)
Closed 3 months ago.
At a complete loss here - trying to match a a colon either side of any given word in a passage of text.
For example:
:wave: Hello guys! :partyface: another huge win for us all to celebrate!
An appropriate regex that would match:
:wave:
:partyface:
Really appreciate your help!
\w*:\b
To catch all the content
:[^:]*:
To catch the content between
(?<=:)[^:]*(?=:)

If character in string put escape character "\" in front of the found character [duplicate]

This question already has answers here:
Replacing instances of a character in a string
(17 answers)
Closed 7 months ago.
I am requesting an API which sometimes gives a string that contain "*" characters.
I have to post the output on discord, where ** makes the text bold.
I want to see if a string contains any * and if so put a markdown \ escape character in front of the *.
How can I accomplish this?
As #Random Davis rightly pointed out in the comments, you can use str.replace("*","\*")and it will replace all the * occurrence.

Put single and double quote in Python string format? [duplicate]

This question already has answers here:
Python string literals - including single quote as well as double quotes in string
(3 answers)
Using quotation marks inside quotation marks
(12 answers)
Closed 3 years ago.
I'm having trouble figuring out how to print a very specific line.
I tried using .format and I also tried the print("str",variable,"str") method but I can't seem to figure out how to make it print correctly.
feet=5
inches=6
print('Room Length:{}' {}"'.format(feet,inches))
I want the computer to print; Room Length: 5' 6"
but I am not sure how to print this while keeping the apostrophe and the quotations for feet and inches.
print('Room Length:{}\' {}\"'.format(feet,inches))

lstrip unexpected output: removes additional character [duplicate]

This question already has answers here:
Python string.strip stripping too many characters [duplicate]
(3 answers)
Closed 5 years ago.
lstrip removed additional character, please help me understand it why. Is it removing all the input characters from the beginning?
'http://twitter.com/c_renwick'.lstrip('http://twitter.com/')
>>>'_renwick'
lstrip takes a list of characters to remove from the string. As c is in the list you provided, it gets removed
To achieve what you actually want, use replace:
'http://twitter.com/c_renwick'.replace('http://twitter.com/','')

What are () (parentheses) are for in regex python [duplicate]

This question already has answers here:
Python regex -- extraneous matchings
(5 answers)
Closed 6 years ago.
I searched in all the internet and didnt get a good answer on this thing.
What parentheses in python are stand for? its very wierd..
For example, if i do:
re.split(r'(/s*)', "ho from there")
its will give me a list of separate words with the spaces between that... how does its happening?
This isn't specific to python, but in regex those denote a capture group.
Further information on how these are handled in re.split can be seen here

Categories