regex - Grepdiff 没有正确匹配行的开头或结尾
问题描述
我对 grepdiff 中的某些行为感到困惑。我正在尝试编写一个脚本来解析差异的各个块并寻找模式。特别是,我正在寻找添加和删除仅对数值进行微小更改的行的差异,如下所示:
@@ -2160 +2160 @@
- "posZ": 13.912,
+ "posZ": 13.911,
我的目标是让脚本恢复这些更改。为此,我需要先检测它们。但是,我无法让 grepdiff 正确识别行。
此正则表达式确实匹配:
git diff -U0 HEAD^ | grepdiff -E '^[^0-9-]*[0-9-]+.[0-9]+,' --output-matching=hunk
这不会:
git diff -U0 HEAD^ | grepdiff -E '^[^0-9-]*[0-9-]+.[0-9]+,$' --output-matching=hunk
同样,这也不是:
git diff -U0 HEAD^ | grepdiff -E '^\+[^0-9-]*[0-9-]+.[0-9]+,' --output-matching=hunk
如果没有正确匹配行的开头或结尾,我看不出如何识别这些帅哥。
解决方案
利用
grepdiff -E '^[^0-9-]*[0-9-]+[.][0-9]+,[[:space:]]*$'
解释
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
[^0-9-]* any character except: '0' to '9', '-' (0
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
[0-9-]+ any character of: '0' to '9', '-' (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
[.] any character of: '.'
--------------------------------------------------------------------------------
[0-9]+ any character of: '0' to '9' (1 or more
times (matching the most amount possible))
--------------------------------------------------------------------------------
, ','
--------------------------------------------------------------------------------
[[:space:]]* any character of: whitespace characters
(like \s) (0 or more times (matching the
most amount possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
推荐阅读
- ibm-mq - 通过 MQ Explorer 在容器中连接远程队列管理器
- python - pygame.mixer - 我录制了一些内容并将其转换为 mp3 文件,但出现错误
- discord - 发送消息后崩溃?
- delphi - TMemo 中的空格与文本文件不匹配
- swift - (UIApplication.shared.keyWindow?.rootViewController as?BaseSlidingController)?.openMenu() return nil Swift 4.2
- jquery - bootstrap 4 轮播控制位置
- python - 基于最频繁值的子集
- java-3d - 多个 Canvas3D 的 OrbitBehavior
- amazon-web-services - Packer Amazon-EBS 构建器示例构建失败:等待 SSH 超时
- c# - FileStream 类 C# 从 txt 文件输入到数组