[英]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
可以具有以下字符串:
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
包含我要查找的10个描述的首字母:
然后在它之前和之后取两个数字,因此我可以分配时间
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]
返回之前的所有数字:
time.rsplit(':', 1)[0]
returns all numbers after :
time.rsplit(':', 1)[0]
返回以下所有数字:
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? 我尝试过使用regex是更好的主意,但这有点复杂吗?
You can try re.finditer()
您可以尝试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 08:59
19:59 19:59
19:55 19:55
08:59 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): 以下正则表达式模式正好匹配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)
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: 如果字符串彼此分开,则可以使用如下的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....'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.