2016-03-27 37 views
4

Python中是否有任何直接的方法去除字符串并获取开始索引和结束索引?剥离一个字符串并获取开始索引和结束索引

示例:给定字符串' hello world! ',我想剥离字符串'hello world!'以及起始索引2和索引14

' hello world! '.strip()只返回剥离的字符串。

我可以写一个函数:

def strip(str): 
    ''' 
    Take a string as input. 
    Return the stripped string as well as the start index and end index. 
    Example: ' hello world! ' --> ('hello world!', 2, 14) 
    The function isn't computationally efficient as it does more than one pass on the string. 
    ''' 
    str_stripped = str.strip() 
    index_start = str.find(str_stripped) 
    index_end = index_start + len(str_stripped) 
    return str_stripped, index_start, index_end 

def main(): 
    str = ' hello world! ' 
    str_stripped, index_start, index_end = strip(str) 
    print('index_start: {0}\tindex_end: {1}'.format(index_start, index_end)) 

if __name__ == "__main__": 
    main() 

但我不知道Python或一个通俗图书馆是否提供任何内置的方式来做到这一点。

+1

我不认为有内置的方式。你的代码非常简洁,实际上只是'str_stripped = str.strip()','index_start = str.find(str_stripped)'和'index_end = index_start + len(str_stripped)'这三行。其余的都是多余的。 –

+1

@LukeTaylor:它很简洁,但正如评论说他正在对字符串进行多次传球。当然,你可以编写一个'strip()'函数,它可以在执行一遍时返回所需的输出。 –

回答

6

一个选项(可能不是最直接的)将与正则表达式来做到这一点:

>>> import re 
>>> s = ' hello world! ' 
>>> match = re.search(r"^\s*(\S.*?)\s*$", s) 
>>> match.group(1), match.start(1), match.end(1) 
('hello world!', 2, 14) 

其中^\s*(\S.*?)\s*$模式:

  • ^是一个字符串
  • 的开始
  • \s*零个或多个空格字符
  • (\S.*?)是一个捕获组,捕获非空格字符后跟任意字符任意次数以non-greedy方式
  • $是一个字符串
+2

如果没有空白被剥离,这将无法正常工作。用'\ s *''代替应该帮助 – schwobaseggl

+0

@schwobaseggl啊,好点,让我解决这个问题。 – alecxe

+0

打算发布类似的东西,它似乎实际上是我做的测试中最快的方法,但只有在您首先编译时,否则它比OP自己的代码慢得多 –

3

的最有效的方式来做到这一点是的一端通过调用lstriprstrip分别。例如:

s = ' hello world! ' 
s2 = s.lstrip() 
s3 = s2.rstrip() 
ix = len(s) - len(s2) 
ix2 = len(s3) + ix 

这给:

>>> s3 
'hello world!' 
>>> ix 
2 
>>> ix2 
14 
>>> 
0

事实上,你有必要的方法来完成这个任务。 stripfindlen都是你需要的。

s = ' hello world! ' 
s1 = s.strip() 
first_index = s.find(s1) 
end_index = first_index + len(s1) - 1 
相关问题