Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trim a string: begin immediately after the first full-stop and end at the last

Tags:

python

string

Is there a way to trim a string to begin and end at specific points?

Here's an example: I would like the string (text) to begin immediately after the first full-stop and end at the last full-stop.

_string = "money is good. love is better. be lucky to have any. can't really have both"

Expected output:

"love is better. be lucky to have any."

My attempt:

import re
pattern = "\.(?P<_string>.*?.*?).\"
match = re.search(pattern, _string)
if match != None:
    print match.group("_string")

My attempt started well but stopped at the second full_stop.

Any ideas on how to arrive at the expected output?

like image 603
Tiger1 Avatar asked Apr 08 '14 06:04

Tiger1


People also ask

How do you trim strings?

The trim() method in Java String is a built-in function that eliminates leading and trailing spaces. The Unicode value of space character is '\u0020'. The trim() method in java checks this Unicode value before and after the string, if it exists then removes the spaces and returns the omitted string.

How do I remove spaces at the start and end of a string in Python?

strip() Python String strip() function will remove leading and trailing whitespaces. If you want to remove only leading or trailing spaces, use lstrip() or rstrip() function instead.

How do you get the last part of a string before a certain character?

Use the lastIndexOf() method to get the substring before the last occurrence of a specific character, e.g. str. substring(0, str. lastIndexOf('_')); . The substring method will return a new string containing the part of the string before the last occurrence of the specified character.


3 Answers

This will work, if there is atleast one dot in the string.

print _string[_string.index(".") + 1:_string.rindex(".") + 1]
#  love is better. be lucky to have any.

If you don't want the space at the beginning, then you strip that like this

print _string[_string.index(".") + 1:_string.rindex(".") + 1].lstrip()
# love is better. be lucky to have any.
like image 107
thefourtheye Avatar answered Oct 22 '22 22:10

thefourtheye


import re
_string = "money is good. love is better. be lucky to have any. can't really have both"
str1 =_string[_string.find(".")+1:]
for i in range(len(str1)-1,0,-1):
if(str1[i]=='.'):
    a=str1[:i+1]
    break
print a
#love is better. be lucky to have any.
like image 32
Nandha Kumar Avatar answered Oct 22 '22 20:10

Nandha Kumar


What about using the .index() and .rindex() methods with string slicing?

string = "money is good. love is better. be lucky to have any. can't really have both"
first_full_stop = string.index('.')
last_full_stop = string.rindex('.')
string = string[first_full_stop+1:last_full_stop+1]

Or you can split by full stops (this one works with any number of full stops):

string = "money is good. love is better. be lucky to have any. can't really have both"
string = string.split('.')
string = string[1:-1]
like image 37
Scorpion_God Avatar answered Oct 22 '22 20:10

Scorpion_God