简体   繁体   中英

get a specific substring after and before a character in a string python

I want to split time from decription strings. desc can have this kind of strings:

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 contains 10 first letters of description I want to find : and then to take two numbers before it and after it, so i can split the time

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

time.rsplit(':', 1)[0] returns all numbers before :

time.rsplit(':', 1)[0] returns all numbers after :

How to define to split only two numbers after and before : ?Is this a good way? Is better idea to use regex, i tried but it's a little complicated?

You can try 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()

Output will be:

08:59
19:59
19:55
08:59

Then from this output you can split the hours and minutes easily

How to define to split only two numbers after and before :

With regex; the following regex pattern matches exactly 2 digits, followed by a colon, followed by exactly two digits (which is literally what you ask for):

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)

Output

hour = 19, min = 55

您可以re.findall()

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

If the strings are separated each other you can use a re.search method like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM