How to remove periods from the middle of sentences [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Is it possible to remove periods from the middle of a string (sentence), leaving the ending period?
The answers that I have seen, basically strip all of the periods.
Remove periods at the end of sentences in python

If I understand correctly, this should do what you want:
import re
string = 'You can. use this to .remove .extra dots.'
string = re.sub('\.(?!$)', '', string)
It uses regex to replace all dots, except if the dot is at the end of the string. (?!$) is a negative lookahead, so the regex looks for any dot not directly followed by $ (end of line).

Related

replace all int's in string with _ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
How can I separate data types in a string or list so they can be set to another character, I assume there is something I have missed but everything i have tried so far has now worked for me. so far have tried so split into list and use a for loop to find every int but I can't find a way to differentiate the data types so it can change every int.
You can use regex :
import re
re.sub("\d", "_", "10 4 2")
\d matches any decimal digit character.

How to remove all characters from a string after two white spaces in python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
The string i needed to format is,
string = "How are you? abcdef"
I need to remove the "abcdef" from the string.
string = string.split(' ')[0]
Edit: Explanation. The line of code above will split the single string into a list of strings wherever there is a double space. It is important to note that whatever is split upon, will be removed. [0] then retrieves the first element in this newly formed list.

Match Before and After a pattern in Python RE [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have the following strings.
string1 = "按照由 GPV 提供的相关报告; 世界卫生组织 WHO 发布的有关研究"
string2 = "\n\n 介绍 INTRODUCTION"
How can I remove the spaces between Chinese characters and English acronyms?
The expected result is:
"按照由GPV提供的相关报告; 世界卫生组织WHO发布的有关研究".
However, the re pattern should not remove the space between 介绍 and INTRODUCTION since there are no Chinese characters on the right side of INTRODUCTION.
If you can use the third-party regex implementation module regex, it supports \p{script} tokens which make this task easy :
\p{Han}+\s+\p{Latin}+\s+\p{Han}+
Python native re's unfortunately doesn't support these.
In order to remove the spaces, use capturing groups to select the surrounding words and refer to those in your replacement pattern :
Match (\p{Han}+)\s+(\p{Latin}+)\s+(\p{Han}+)
Replace by \1\2\3

Regex Matching - Python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I need to write a regex matching pattern code to either return true if there is one '+' between two words and nothing else. I have written the code to check if there is only one '+' in the string but how will I check it is between two words?
The code is below:
import re
inputStr= "ali+ahmedafaw+"
inputStr2= "hello+world+again"
plus=re.findall(r'[+]', inputStr)
print (plus)
l_plus=len(plus)
print "The length is ",l_plus
if l_plus<=1:
print "True"
else:
print "False"
Actually it depends on what you mean by word. If you mean a word with more than one character, you can simply use [a-zA-Z]+ around the + character. Or other patterns which will match different characters like \w to match word characters.
re.search(r'[a-zA-Z]+\+[a-zA-Z]+', input_str)
But if you just want it doesn't appears at the leading and trailing of your text you can use negative look-around:
re.search(r'(?<!^)\+(?!$)', input_str)

Regex in python to catch a string like MMS-2839 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
How can I catch some string like these with a regex in python?
M1Sxs-2839
McS-28S9213
Both the first and the second part (divided by the -) can contains letters and numbers (case insensitive).
You may try the below re.match function.
re.match(r"(?i)[A-Z0-9]+-[A-Z0-9]+$", st)
(?i) helps to do case-insensitive match. Since re.match scans the input from start, you don't need to add start of the line anchor ^ explicitly.

Categories

Resources