如何使用正则表达式提取此字符串坐标的 3 个数值,无论它们是正数坐标还是负数坐标
How to extract with a regex the 3 numerical values of coordinates of this string, whether they are positive or negative numerical coordinates
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
如果您的值少于 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
如果您想捕获 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')
根据您展示的样本,从 Thefourthbird 的回答中汲取灵感;请也尝试使用正则表达式。
^".*?:\s*(-?\d+(?:\.\d+)?),\D*?(-?\d+(?:\.\d+)?).*?(-?\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).
,\D*? ##Matching 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).
.*? ##Mentioning lazy match here.
(-?\d+(?:\.\d+)?) ##Creating 3rd capturing group which matches optional - here, 1 or more digits followed by optional .digits(to catch floating numbers).
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
如果您的值少于 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
如果您想捕获 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')
根据您展示的样本,从 Thefourthbird 的回答中汲取灵感;请也尝试使用正则表达式。
^".*?:\s*(-?\d+(?:\.\d+)?),\D*?(-?\d+(?:\.\d+)?).*?(-?\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).
,\D*? ##Matching 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).
.*? ##Mentioning lazy match here.
(-?\d+(?:\.\d+)?) ##Creating 3rd capturing group which matches optional - here, 1 or more digits followed by optional .digits(to catch floating numbers).