【问题标题】:How to extract with a regex the 3 numerical values of coordinates of this string, whether they are positive or negative numerical coordinates如何使用正则表达式提取此字符串的坐标的 3 个数值,无论它们是正数值坐标还是负数值坐标
【发布时间】:2022-01-23 10:45:19
【问题描述】:
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)

虽然代码运行不好,但万一它们是负坐标或者字符串末尾有更多值。 我应该如何更正这个正则表达式,以便它可以捕获我在代码中输入的示例值?

“坐标:@, 63, -5|hhhf♀” ----> 这应该提取 0,63,-5

“坐标:@, 63.5, -5.695|hhhf♀” ----> 这应该提取 0,63.5,-5.695

“坐标:@, hhkjkm♀-63ss, -5|hhhf♀” ----> 这应该提取 0, -63, -5

"Coordenadas: -8, 63, -5 \n♀" ----> 这应该提取 -8,63,-5

"Coordenadas: @, 63, -5" ----> 这应该提取 0,63,-5

【问题讨论】:

  • 第一个问题很好 - 感谢您花时间提供代码、示例和预期结果。

标签: python python-3.x regex string re


【解决方案1】:

如果您的值少于 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

【讨论】:

    【解决方案2】:

    如果您想在 3 个捕获组中捕获所有 3 个值(您的代码将 @ 替换为 0),您可以:

    • 省略锚点$ 以断言字符串的结尾
    • 匹配一个可选的-
    • 请注意,并非所有示例中的逗号后面都有一个数字,如@9​​87654325@

    模式可能如下所示:

    ^[^:]*:\s*(-?\d+(?:\.\d+)?),\D*?(-?\d+(?:\.\d+)?)\D*?(-?\d+(?:\.\d+)?)
    

    Regex demo

    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')
    

    【讨论】:

      【解决方案3】:

      通过您展示的样本,从 Thefourthbird 的回答中汲取灵感;请尝试以下正则表达式。

      ^".*?:\s*(-?\d+(?:\.\d+)?),\D*?(-?\d+(?:\.\d+)?).*?(-?\d+(?:\.\d+)?)
      

      Online demo for above regex

      说明:为上述添加详细说明。

      ^".*?:\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).
      

      【讨论】:

      • 我认为你错过了最后一组中的-
      • @Thefourthbird,谢谢您的评论,正则表达式 .*?[-,] 不匹配第 3 组之前的内容?
      • 非数字应该是非贪婪的\D*?,否则你会错过第2组中的-。对于第3组,连字符在组之外[-,](\d+(?:\.\d+)?)
      • 我觉得是这样的^".*?:\s*(-?\d+(?:\.\d+)?),\D*?(-?\d+(?:\.\d+)?).*?(-?\d+(?:\.\d+)?)
      • @Thefourthbird,谢谢,我现在已经编辑了,希望我编辑得很好,干杯。
      猜你喜欢
      • 2021-01-17
      • 1970-01-01
      • 1970-01-01
      • 2021-06-17
      • 2020-12-19
      • 1970-01-01
      • 2018-12-19
      • 1970-01-01
      • 2022-07-11
      相关资源
      最近更新 更多