我无法通过bash来匹配我的正则表达式 [英] I am having trouble getting bash to match my regex
问题描述
我正在学习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屋!