我无法通过bash来匹配我的正则表达式 [英] I am having trouble getting bash to match my regex

查看:98
本文介绍了我无法通过bash来匹配我的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习bash脚本,我试图理解正则表达式如何与bash一起工作。在我的简单程序中,我有一个各种不同行的文件,但想要匹配一个简单的int对,用逗号和空格分隔的两个数字的格式如下:123,123。我也想匹配行这样我就可以轻松地从行中提取第一个和最后一个数字。但是,当我将一个示例文件传递给我的程序时,它与我期望的任何行都不匹配。



我尝试了什么:



#!/ bin / bash 

re =([0-9] + )(,)([0 = 9] +)

echo $ 1
file1 = $ 1

而IFS = read -r line; do
echo行是:$ line
if [[$ line =〜$ re]];然后
echo $ {BASH_REMATCH [0]};
echo $ {BASH_REMATCH [1]};
echo $ {BASH_REMATCH [2]};
fi
完成

解决方案

1
file1 =


< blockquote> 1

而IFS = read -r line; do
echo行是:



如果[[


I am learning bash scripting and I am trying to understand how regex works with bash. In my simple program, I have a file of various different lines but want to match a simple int pair with the format of the two numbers separated by a comma and a space as so: 123, 123. I also want to match the line with groups so I can easily extract the first and last numbers from the line. But when I pass a sample file to my program, it doesn't match any of the lines that I expect.

What I have tried:

#!/bin/bash

re="([0-9]+)(, )([0=9]+)"

echo $1
file1=$1

while IFS= read -r line; do
        echo "The line is: $line"
        if [[ $line =~ $re ]]; then
               echo ${BASH_REMATCH[0]};
               echo ${BASH_REMATCH[1]};
               echo ${BASH_REMATCH[2]};
        fi
done

解决方案

1 file1=


1 while IFS= read -r line; do echo "The line is:


line" if [[


这篇关于我无法通过bash来匹配我的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆