regex 使用正则表达式从字符串中获取数学表达式

1aaf6o9v  于 2023-06-30  发布在  其他
关注(0)|答案(1)|浏览(123)

我有一堆字符串如下:

0 + (1/4 - sqrt(5)/4)*i + 1/2*j + (1/4 + sqrt(5)/4)*k
1 + 0*i + 0*j + 0*k
1/2 + 1/2*i + 1/2*j + 1/2*k

我想从这些字符串中提取数字和数学表达式。
我写了一个函数。它适用于第二和第三行,但不适用于第一行:下面是我的函数:

import math
import re
import numpy as np
matrices_of_icos = []
from fractions import Fraction
pattern = r"[-+]?(?:\d+(?:/\d+)?)|(?:sqrt\(\d+\))"
#pattern = r"[-+]?(?:\d+(?:/\d+)?)|(?:sqrt\(\d+\))|\((?:[-+]?(?:\d+(?:/\d+)?))?(?:[+-]\s*sqrt\(\d+\))?\)"
for i in saving_icos:
    numbers = []
    string_ico = str(i)
    print(string_ico)
    # Find all matches of the pattern in the string
    matches = re.findall(pattern, string_ico)
   
    for match in matches:
        #with_par = re.findall(r'\(([\S]*?)\)(?=\s|$)', match)
        #print(with_par)
        if "/" in match:
        # Fraction case: convert string to Fraction object
            number = float(Fraction(match))
            numbers.append(number)
        elif "sqrt" in match:
        # Square root case: extract the number inside sqrt and calculate square root
             num = int(re.search(r"\d+", match).group())          
             number = math.sqrt(num)
             numbers.append(number)
        else:
        # Integer or decimal case: convert string to float
             number = float(match)
             numbers.append(number)
    print(numbers)

第一行代码的输出如下:[0.0,0.25,2.23606797749979,4.0,-0.25,2.23606797749979,4.0,0.5]如何泛化代码以正确查找第一行?它应该是[0,0.8,0.3,0.5]提前感谢

hgtggwj0

hgtggwj01#

这不能仅用正则表达式来完成,您首先需要匹配括号,以便首先计算它们的内部值。
如果你想评价,例如(1/4 - sqrt(5)/4)如果使用您自己的代码,则需要首先计算sqrt(5),然后计算1/4sqrt(5)/4,之后可以计算最终值。
如果你知道你的ICO总是遵循上面的格式,这可能会起作用:

from math import sqrt
import re

for icos in saving_icos:
    parts = []
    open_parenths = 0
    part = ""
    for char in icos:
        if char == "(":
            open_parenths += 1
        if char == ")":
            open_parenths -= 1

        if char == "+" and open_parenths == 0:
            parts.append(part.strip())
            part = ""
        else:
            part += char

    parts.append(part.strip())

    values = []
    for part in parts:
        part = re.sub(r"\*[ijk]", "", part)
        values.append(eval(part))

    print(values)

它首先提取部分,跟踪它传递了多少个左括号和/或右括号,然后使用python内置的eval来获取值。注意它使用

from math import sqrt

因为sqrt(5)的求值是作为python代码来求值的,并且使用了sqrt()函数。

相关问题