打印关键字后面的5个字符

时间:2017-02-19 15:08:37

标签: python string

我想创建一个简单的代码,它接受一段文本,扫描关键字并打印关键字以及接下来的5个字符。请注意,关键字可以在文本中出现多次。

  string = 'my name is luka 90/91, I live on the second floor'
    keyword = 'luka'

    if key in string:
        print (key + key[0:5])

输出应该是luka 90 \ 91

3 个答案:

答案 0 :(得分:5)

使用str.find,您可以获得匹配字符串的索引:

>>> string = 'my name is luka 90/91, I live on the second floor'
>>> keyword = 'luka'
>>> string.find(keyword)
11
>>> i = string.find(keyword)
>>> string[i:i+len(keyword)+5]
'luka 90/9'
>>> string[i:i+len(keyword)+5+1]  # +1 (count space in between)
'luka 90/91'

UPDATE 要获取所有匹配项,您需要在循环中找到子字符串。

string = 'my name is luka 90/91, I live on the second floor luka 12345'
keyword = 'luka'

i = 0
while True:
    i = string.find(keyword, i)  # `i` define from where the find start.
    if i < 0:
        break
    j = i + len(keyword) + 5 + 1
    print(string[i:j])
    i = j

更新解决方案使用re.findall

>>> string = 'my name is luka 90/91, I live on the second floor luka 12345'
>>> keyword = 'luka'
>>> import re
>>> re.findall(re.escape(keyword) + '.{5}', string)
['luka 90/9', 'luka 1234']
>>> re.findall(re.escape(keyword) + '.{6}', string)
['luka 90/91', 'luka 12345']
  • luka字面上匹配。 .{5}匹配以下任意5个字符。
  • 如果您想匹配字符,即使它们少于5个字符。请改用.{1,5}
  • {li} re.escape不是luka所必需的。如果特殊字符在正则表达式中具有特殊含义,则需要它。

答案 1 :(得分:1)

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl', function ($scope) {
    $scope.data = {
      "FullName": "Lucy Ann Johnson",
      "WentMissing": "1961",
      "Age:": "20",
      "Description": "Short brunette, was last seen wearing a green dress.",
      "Image": "iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAAY8ElEQVR4Xu2dCbxVVb3HucwagzY9QUsInmQEMlxAJAuemAxqWiIaoCYKUpZAgz2rT74+vQYVhzRABIKMNMRARdEEonyIzAThqye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+BgguNREyrTKX2M9rsn38kcT5w/AteXyqPdM6EpznlQDQILDFDdB2HMBgTfWX6BMyftohw4dRsYFgZw/Vi13Ul9gCH0q/yWt/4Qw8WlWdBBtqQeACwIFeEC5nzcU8lxi746IE1zZxPnT9DXnPXSJc9mnsgCApMzooDGxARWfxzdIk48m5gGCq21BEOX8AcI/42sojGydSGUDABcEBJT8FUoYbih9XYU3HHN92CQ/yteOpaggTrdQ3s9MyiuHPGUFAAlUFzxu3bpVQSeuNhTwE0wuXWly1t/E+eMI3OmbNm36p2Hdqc9WdgDwgGA2341uOsV/eGr//v1XhIHAxPlzhpoFvUCr2IgpSwA4QlLMH91jaKQQlPf07t27vxAUYtZwz9/5mP+kL4Eqts5r1VfOABAjDVHcdJRrNB9PvkWEhvl87m1gLPkqRIz2/IVdJPUyyjc96VRSpdpUXu4AEK+69ELBl422Yzt7+27jDp4X3nnnnZM4wjaE337C+6FBo3ieyGlmG+UUI29dAEAGBFiCqWFLt/kIU3v+cP7OqEvOnyuPugIA1xIo8mchbuK4D/OfvTkkHzCl7d26BAAXBIo8YhUMMkIpb3GT2dlcZfdu2pSXBD11DQAZmeATaKIm732DCnuLYziE1m9yu3oS+ih6GXUSAJIic/p349zF3kbuKH80ytd8Q51NdRYAjiXQZQ938gm95ydXuyj/HVr+tSg/ketw0oyeOg0ACd6JH3CnTHmUInTxBXlmc2rpO9zpp4ua63yq8wBwNejEEBiOcnX+oBOfDwIKXUH/d7qKTfy/mPOKjxbyouw0oul/AWddtKbhiDSoAAAAAElFTkSuQmCC"
    };
});

或者更好的解决方案: import re string = 'my name is luka 90/91, I luka onthe second floor' keyword = 'luka' i = [m.start() for m in re.finditer(keyword, string)] K = [[keyword + ' '+ string[len(keyword)+1+j:len(keyword)+j+5+1]] for j in i]

L = re.findall(keyword + ' .{5}', string)

<强>输出:

print(K)

[['luka 90/91'], ['luka onthe']]

<强>输出:

print(L)

我为空格添加了额外的1,并且在['luka 90/91', 'luka onthe']足够大之后假设关键字。否则会出现空格。如果这是不需要的,可以修复。但是这种行为并不是由你定义的。

答案 2 :(得分:0)

string = 'my name is luka 90/91, I live on the second floor. luka means nothing'
keyword = 'luka'
split_array = string.split(keyword)
for x in split_array[1:]: # If keyword is found split array would contain more than one element
   print keyword, x.strip()[:5]

<强>输出

luka 90/91
luka means