r - 如何从字符串中仅提取有效方程
问题描述
提取以下文本中的所有有效方程。
我尝试了一些正则表达式,但似乎都没有。希望在 R 中使用sub
orgsub
功能。
myText <- 'equation1: 2+3=5, equation2 is: 2*3=6, do not extract 2w3=6'
expected result : 2+3=5 2*3=6
解决方案
这是一个基本的 R 方法。我们可以使用grepexpr()
在输入字符串中查找多个方程匹配:
x <- c("equation1: 2+3=5, equation2 is: 2*3=6, do not extract 2w3=6")
m <- gregexpr("\\b\\w+(?:[+-\\*]\\w+)+=\\w+\\b", x)
regmatches(x, m)
[[1]]
[1] "2+3=5" "2*3=6"
这是正则表达式的解释:
\\b\\w+ match an initial symbol
(?:[+-\\*]\\w+) then match at least one arithmetic symbol (+-\*) followed
by another variable
+=\\w+ match an equals sign, followed by a variable
推荐阅读
- r - 我需要向量化函数创建向量
- python - 数组斯托克韦尔变换 - 元组索引超出范围
- r - 提取行并计算平均值
- autodesk-forge - 通过 id 设计自动化获取 AppBundle
- java - Intellij:更改 ctrl+space 弹出背景
- php - 当用户登录到 Azure 应用服务时,PHP $_SESSION 变量被替换(免费:计划)
- python - 使用 scipy.odeint 求解预定义函数的非线性 ODE
- mysql - 您无权操纵用户
- python - 如何修复 ID 为“3/Ewurafua/processed.cleveland.data.csv”的 csv 上传不存在。也许它被删除了?
- android - 进度对话框在活动完成绘制 UI 之前关闭