在字符串python中的字符之前和之前获取特定的子字符串

时间:2015-12-10 10:38:12

标签: python regex split string-split

我想从描述字符串中分割时间。 desc可以有这种字符串:

desc = '08:59 Hudh aes ....' 
desc = '19:59Aksi jeh....' 
desc = 'just letters without time'
desc = '21.19:55Aksi hsue....'
desc = '256.08:59Aksi mane....'

time包含10个首字母描述 我想找到:,然后在它之前和之后取两个数字,这样我就可以分时间了

time = ''.join(filter(lambda x: x, desc[0:10].replace(" " , "")))
print  "Time:" , time , time.rsplit(':', 1)[0]

time.rsplit(':', 1)[0]会返回:

之前的所有数字

time.rsplit(':', 1)[0]会返回:

之后的所有数字

如何定义在:之前和之后仅拆分两个数字?这是一个好方法吗?使用正则表达式更好的主意,我试过但它有点复杂?

4 个答案:

答案 0 :(得分:1)

您可以尝试re.finditer()

 test = """desc = '08:59 Hudh aes ....' 
 desc = '19:59Aksi jeh....' 
 desc = 'just letters without time'
 desc = '21.19:55Aksi hsue....'
 desc = '256.08:59Aksi mane....'
 """
 pattern = r"\d{2}:\d{2}"
 for m in re.finditer(pattern,test):
    print m.group()

输出将是:

  

8时59分
  19:59
  19:55
  08:59

然后,您可以从此输出中轻松分割小时和分钟

答案 1 :(得分:1)

  

如何定义在之前和之后仅拆分两个数字:

正则表达式;以下正则表达式模式匹配完全 2位数,后跟冒号,后跟完全两位数字(这就是你要求的字面意思):

import re

desc = '21.19:55Aksi hsue....'
m = re.search(r'(\d{2}):(\d{2})', desc)
if m:
    hour, min = m.groups()
else:
    # no time in desc string
    pass
print 'hour = {}, min = {}'.format(hour, min)

<强>输出

hour = 19, min = 55

答案 2 :(得分:0)

您可以re.findall()

re.findall(r'([\d.]+):([\d.]+)',desc)

答案 3 :(得分:0)

如果字符串彼此分开,您可以使用re.search方法,如下所示:

import re
[hour, min, desc] = re.search(r'(?:(?:\d+\.)?(\d{2}):(\d{2}))?[ \t]*(.*)',desc).groups()

# --
# desc  = '08:59 Hudh aes ....'
# hour -> '08'
# min  -> '59'
# desc -> 'Hudh aes ....', the space before the description are stripped too
# --
# desc  = '19:59Aksi jeh....'
# hour -> '19'
# min  -> '59'
# desc -> 'Aksi jeh....'
# --
# desc  = 'just letters without time'
# hour -> None
# min  -> None
# desc -> 'just letters without time'
# --
# desc = '256.08:59Aksi mane....'
# hour -> '08'
# min  -> '59'
# desc -> 'Aksi mane....'

Online regex demo