regex - 匹配正则表达式中表格中的数字

标签 regex vim windows-xp

不要介意奇怪的编码。

有没有一种方法(在这样的表格中,以 OF DAMAGE STABILITY 开头)并以“Flooding percentage”行结尾,以仅匹配最后一列中小于某个值(例如,0.018)的数字?

我正在练习正则表达式,但现阶段这对我来说太多了。所以我希望有人至少可以推荐什么是最好的解决方法。

                    SUMMARY OF RESULTS OF DAMAGE STABILITY                      
 ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
       DAMAGE CASE               %  R  HEEL   GM     FBmin   GZ>0  GZmax  Area  
                                      (deg)   (m)     (m)   (deg)   (m)  (m.rad)
 ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
 109.10s                        100 1  -4.1   0.438   1.243  59.2  0.133  0.021 
                                 20 1  -6.6   0.740   2.215  63.4  0.479  0.049 
                                 40 1  -5.8   0.721   2.372  64.2  0.393  0.045 
                                 60 1  -3.2   0.728   2.537  66.8  0.277  0.041 
                                 80 1  -0.8   0.721   2.354  66.8  0.192  0.037 

 109.10p                        100 1  -4.1   0.438   1.243  59.2  0.133  0.021 
                                 20 1   4.5   0.688   2.494  65.5  0.507  0.049 
                                 40 1   3.7   0.684   2.580  66.3  0.417  0.046 
                                 60 1   1.1   0.720   2.599  68.9  0.300  0.043 
                                 80 1  -1.2   0.693   2.350  65.2  0.177  0.035 


 110.10s                        100 1  -3.0   0.748   1.837  39.7  0.494  0.049 
                                 20 1  -2.9   0.777   2.333  43.1  0.594  0.052 
                                 40 1  -2.9   0.777   2.333  42.5  0.576  0.052 
                                 60 1  -2.9   0.756   2.294  41.6  0.551  0.050 
                                 80 1  -2.9   0.748   2.077  40.7  0.520  0.049 

 110.10p                        100 1  -0.5   0.733   2.143  41.6  0.484  0.047 
                                 20 1  -0.5   0.763   2.627  45.3  0.613  0.051 
                                 40 1  -0.5   0.763   2.627  44.7  0.592  0.051 
                                 60 1  -0.5   0.740   2.593  43.8  0.558  0.049 
                                 80 1  -0.5   0.767   2.372  42.7  0.516  0.048 


 %     : Flooding percentage.                                                   
 R     : R=1 if run-off weights considered, R=0 if no run-off.                  
 HEEL  : Heel at equilibrium (negative if equilibrium is on port).              
 GM    : GM at equilibrium.                                                     
 FBmin : Minimum distance of margin line, weathertight or non-weathertight      
         points from waterline.                                                 
 GZ>0  : Range of positive GZ limited to immersion of non-weathertight openings.
 GZmax : Maximum GZ value.                              

最佳答案

0.018这个例子不好,因为最后一列中的所有数字都大于 0.018 ....我花了几分钟才找到它。我以为我的 vim 替换命令有问题...

查看示例,给定的范围边界(以 OF DAMAGE STABILITY 开始并以“Flooding percentage”结束)不是必需的,但您最了解要求。我也在命令中添加了范围。

在下面的命令中,我将其更改为 0.040这样我们就可以看到差异。下面的命令将添加一个 *在匹配号码的末尾。

 /STABILITY/,/Flooding/s/\v([0-9.-]+)\s*$/\=str2float(submatch(1))<0.040? submatch(1).'*':submatch(1)

简要说明:

 /STABILITY/,/Flooding/                                      " fit the range, as you defined
 s/                                                          " start substitution
 \v                                                          " use very magic, so that I could save some escaping
 ([0-9.-]+)\s*$/                                             " match the last number column (float or negative)
 \=str2float(submatch(1))<0.040? submatch(1).'*':submatch(1) " vimscript expression, if the value<0.040, add a '*'

如果你在 linux box 上,这个工作也很容易通过 awk 完成。如果您是 Microsoft 用户,Excel 可能(我不确定)是个好工具。

编辑

从您的评论来看,我认为您没有明白我的意思。我

命令:

 /STABILITY/,/Flooding/s/\v([0-9.-]+\d)\s*$/\=str2float(submatch(1))<0.040? submatch(1).'<':submatch(1)/

然后你进行搜索:

/[0-9.-]\+\d\ze<$

注意这突出显示了数字(我仍然使用 0.040 来查看差异),同时添加标志/后缀 < .您可以按 n 移动到下一个突出显示的号码.上一个 N .如果你完成“阅读”文件,退出 :q!ZQ跳过更改。

希望这能满足您的要求。

看这个例子: enter image description here

关于regex - 匹配正则表达式中表格中的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16560180/

相关文章:

jquery检查元素内容后的字符出现情况

regex - python 替换字符串函数抛出 asterix 通配符错误

vim - 如何对齐代码的参数列?

具有多线程的 Windows 上的 Python Popen - 无法删除标准输出/标准错误日志

用 win7 编写的 winforms 应用程序在 win xp 上看起来不同。为什么?

regex - bash 。如何在标签之间获取多行文本

java - 密码与正则表达式匹配

java - 无法从一台计算机远程接收数据

spell-checking - 切换到插入模式时是否可以切换 vim 选项?

vim - 向上拉 n 行而不移动