简体   繁体   English

如何从我的字符串中检索子字符串?

[英]How to retrieve a substring from my string?

The string variable myvar can have the following values: 字符串变量myvar可以具有以下值:

261.30 (NM) / 300.76 (MI) / 483.93 (KM)
952.27 (NM) / 1,096.09 (MI) / 1,763.61 (KM)

I need to extract the numbers 483.93 and 1,763.61 . 我需要提取数字483.931,763.61 These numbers should be converted to float numbers or rounded up to integers. 这些数字应转换为浮点数或向上舍入为整数。

This is what I tried: 这是我试过的:

mylar = "261.30 (NM) / 300.76 (MI) / 483.93 (KM)"
int(myvar[28:-8])

It works in the first case ( 261.30 (NM) / 300.76 (MI) / 483.93 (KM) ). 它适用于第一种情况( 261.30 (NM) / 300.76 (MI) / 483.93 (KM) )。 But it fails in case of 952.27 (NM) / 1,096.09 (MI) / 1,763.61 (KM) . 但在952.27 (NM) / 1,096.09 (MI) / 1,763.61 (KM)情况下它失败了。 Is there any generic solution? 有没有通用的解决方案?

You can use this regex that detects the last number in the string and places it in group1, 您可以使用此正则表达式检测字符串中的最后一个数字并将其放在group1中,

([^ ]*)[^\d]*$

Demo 演示

Then with following python code, you can convert it into float or int. 然后使用以下python代码,您可以将其转换为float或int。 I am converting it to float as they are decimal values but you can easily cast them to int. 我将它转换为float,因为它们是十进制值,但您可以轻松地将它们转换为int。

import re

arr = ['261.30 (NM) / 300.76 (MI) / 483.93 (KM)','952.27 (NM) / 1,096.09 (MI) / 1,763.61 (KM)']

for s in arr:
 val = re.search(r'([^ ]*)[^\d]*$', s)
 floatval = float(re.sub(r',','',val.group(1)))
 print('Float value: ' + str(floatval))
 print('Int value: ' + str(int(floatval)))

Prints, 打印,

Float value: 483.93
Int value: 483
Float value: 1763.61
Int value: 1763

With single re.search function, without any replacement: 使用单个re.search功能,无需任何替换:

import re

myvar = '952.27 (NM) / 1,096.09 (MI) / 1,763.61 (KM)'
num = re.search(r'\/\s+(\d+),?(\d+\.\d+)\s+\(KM\)', myvar)
if num:
    num = float(num.group(1) + num.group(2))

print(num)

The output: 输出:

1763.61

You could also achieve the same by using only split() 你也可以通过只使用split()来实现同样的目的

str1="261.30 (NM) / 300.76 (MI) / 483.93 (KM)"
last_num_string=str1.split('/')[2].split('(')[0]
last_num_float=float(last_num_string)
print(last_num_float)

What I would do in this case is create a list of the values, from which I can extract any of those. 在这种情况下我要做的是创建一个值列表,我可以从中提取任何值。

myvar2 = myvar.replace("/", "").replace(",", "")
myvar2 = myvar2.split()
# This gives a list like this: ["261.30","(NM)","300.76","(MI)","483.93","(KM)"]
myfloat = float(myvar[4]) # This will return 483.93 on the first string and 1,763.61 on the second.

You can do it with regular expression. 你可以用正则表达式来做。

import re

target = '952.27 (NM) / 1,096.09 (MI) / 1,763.61 (KM)'

regex = r'.* \(NM\) \/ .* \(MI\) \/ (.*) \(KM\)'

res_str = re.findall(regex, target)
float_str = res_str[0].replace(',', '') 
result = float(float_str)
import re
m="261.30 (NM) / 300.76 (MI) / 483.93 (KM)"
print(float(re.split('\/',re.sub(',','',m))[2][:-5]))

Since the values are followed by (KM) you could use a positive lookahead: 由于值后跟(KM)您可以使用正向前瞻:

\S+(?= \(KM\))
  • \\S+ Match 1+ non whitespace characters \\S+匹配1+非空白字符
  • (?= \\(KM\\)) Positive lookahead to check what is on the right is a space and (KM) (?= \\(KM\\))正面检查右边的是空格和(KM)

For example: 例如:

regex = r"\S+(?= \(KM\))"
strings = ["261.30 (NM) / 300.76 (MI) / 483.93 (KM)", "952.27 (NM) / 1,096.09 (MI) / 1,763.61 (KM)"]

for s in strings:
    matches = re.search(regex, s)
    if matches:
        print(float(matches.group().replace(',', '')))

That will give you: 那会给你:

483.93
1763.61

Regex demo | 正则表达式演示 | Python demo Python演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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