import asyncio
import re
import time
from datetime import datetime
detection_timer = 0
detection_timer_increment = 5
detection_timer_change = 10
x, y , z = None, None, None
x_aux, y_aux, z_aux = 0, 0, 0
def get_coords(input_coords):
input_coords = input_coords.replace("@","0") #convierte todos los posibles caracteres @ en caracteres 0
m = re.match(r".*:\s*([0-9.]*?)\s*,\s*([0-9.]*?)\s*,\s*([0-9.]*?)$", input_coords) #No agarra los numeros negativos
if m:
return m.groups()
async def timer():
global x, y, z, x_aux, y_aux, z_aux
global input_coords
global detection_timer, detection_timer_change
detection_timer = detection_timer_increment
#Debe entrar a este if cara cierto tiempo
if(detection_timer >= detection_timer_change):
detection_timer = 0 #resetea contador
#detect_color()
r = get_coords(input_coords)
if r:
x_aux = x = float(r[0]) if r[0] else x
y_aux = y = float(r[1]) if r[1] else y
z_aux = z = float(r[2]) if r[2] else z
return x_aux, y_aux, z_aux
while True:
#Some examples of possible inputs
#input_coords = "Coordenadas: @, 63, -5|hhhf♀"
#input_coords = "Coordenadas: @, 63.5, -5.695|hhhf♀"
#input_coords = "Coordenadas: @, hhkjkm♀-63ss, -5|hhhf♀"
#input_coords = "Coordenadas: -8, 63, -5 \n♀"
input_coords = "Coordenadas: @, 63, -5"
x_aux, y_aux, z_aux = asyncio.run(timer())
if(x_aux != None and y_aux != None and z_aux != None):
print(x_aux)
print(y_aux)
print(z_aux)
雖然代碼不能很好地作業,但如果它們是負坐標或字串末尾有更多值。我應該如何更正這個正則運算式,以便它可以捕獲我放在代碼中的示例值?
"Coordenadas: @, 63, -5|hhhf♀" ----> 這應該提取 0,63,-5
"Coordenadas: @, 63.5, -5.695|hhhf♀" ----> 這應該提取 0,63.5,-5.695
"Coordenadas: @, hhkjkm♀-63ss, -5|hhhf♀" ----> 這應該提取 0, -63, -5
"Coordenadas: -8, 63, -5 \n♀" ----> 這應該提取 -8,63,-5
"Coordenadas: @, 63, -5" ----> 這應該提取 0,63,-5
uj5u.com熱心網友回復:
如果您的值少于 3 個,則只需查找數字并在左側填充零,您就可以簡單地做到這一點:
s = "Coordenadas: @, hhkjkm♀-63ss, -5|hhhf♀"
import re
l = re.findall('-?\d ', s)
out = [0]*(3-len(l)) list(map(int, l)
輸出: [0, -63, -5]
注意。如果您期望十進制值,請使用'-?\d (?:\.\d*)?'和float
uj5u.com熱心網友回復:
如果您想在 3 個捕獲組中捕獲所有 3 個值(您的代碼將替換@為 0),您可以:
- 省略錨
$以斷言字串的結尾 - 匹配一個可選的
- - 請注意,并非在所有示例中,逗號后都有一個數字,例如
hhkjkm♀-63ss, -5|hhhf♀
該模式可能如下所示:
^[^:]*:\s*(-?\d (?:\.\d )?),\D*?(-?\d (?:\.\d )?)\D*?(-?\d (?:\.\d )?)
正則運算式演示
import re
def get_coords(input_coords):
input_coords = input_coords.replace("@", "0")
m = re.match(r"^[^:]*:\s*(-?\d (?:\.\d )?),\D*?(-?\d (?:\.\d )?)\D*?(-?\d (?:\.\d )?)", input_coords)
if m:
return m.groups()
strings = [
"Coordenadas: @, 63, -5|hhhf♀",
"Coordenadas: @, 63.5, -5.695|hhhf♀",
"Coordenadas: @, hhkjkm♀-63ss, -5|hhhf♀",
"Coordenadas: -8, 63, -5 \n♀",
"Coordenadas: @, 63, -5"
]
for s in strings:
print(get_coords(s))
輸出
('0', '63', '-5')
('0', '63.5', '-5.695')
('0', '-63', '-5')
('-8', '63', '-5')
('0', '63', '-5')
uj5u.com熱心網友回復:
使用您展示的樣本,從 Thefourthbird 的回答中汲取靈感;請嘗試遵循正則運算式。
^".*?:\s*(-?\d (?:\.\d )?),\s*\D*(-?\d (?:\.\d )?)\s*.*?[-,](\d (?:\.\d )?)
上述正則運算式的在線演示
說明:為以上添加詳細說明。
^".*?:\s* ##Matching from starting of value " followed by lazy match to match till 1st occurrence of : followed by 0 or more occurrences of spaces.
(-?\d (?:\.\d )?) ##Creating 1st capturing group which has optional - as a match followed by 1 or more digits followed by optional .digits(to catch floating numbers).
,\s*\D* ##Matching comma followed by 0 or more spaces followed by non-digits(0 or more occurrences of it).
(-?\d (?:\.\d )?) ##Creating 2nd capturing group which has - as optional match followed by by 1 or more digits followed by optional .digits(to catch floating numbers).
\s*.*?[-,] ##Matching 0 or more spaces followed by lazy match of either - OR , here.
(\d (?:\.\d )?) ##Creating 3rd capturing group which matches 1 or more digits followed by optional .digits(to catch floating numbers).
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/389570.html
下一篇:條件下替換NA
