Return a regex match in a Bash script, instead of

2020-05-16 02:45发布

I just want to match some text in a Bash script. I've tried using sed but I can't seem to make it just output the match instead of replacing it with something.

echo -E "TestT100String" | sed 's/[0-9]+/dontReplace/g'

Which will output TestTdontReplaceString.

Which isn't what I want, I want it to output 100.

Ideally, it would put all the matches in an array.

edit:

Text input is coming in as a string:

newName()
{
 #Get input from function
 newNameTXT="$1"

 if [[ $newNameTXT ]]; then
 #Use code that im working on now, using the $newNameTXT string.

 fi
}

9条回答
闹够了就滚
2楼-- · 2020-05-16 03:23

You could do this purely in bash using the double square bracket [[ ]] test operator, which stores results in an array called BASH_REMATCH:

[[ "TestT100String" =~ ([0-9]+) ]] && echo "${BASH_REMATCH[1]}"
查看更多
\"骚年 ilove
3楼-- · 2020-05-16 03:25

I don't know why nobody ever uses expr: it's portable and easy.

newName()
{
 #Get input from function
 newNameTXT="$1"

 if num=`expr "$newNameTXT" : '[^0-9]*\([0-9]\+\)'`; then
  echo "contains $num"
 fi
}
查看更多
放我归山
4楼-- · 2020-05-16 03:31

using just the bash shell

declare -a array
i=0
while read -r line
do
        case "$line" in
            *TestT*String* )
            while true
            do
                line=${line#*TestT}
                array[$i]=${line%%String*}
                line=${line#*String*}
                i=$((i+1))
                case "$line" in
                    *TestT*String* ) continue;;
                    *) break;;
                esac
            done
            esac
done <"file"
echo ${array[@]}
查看更多
Bombasti
5楼-- · 2020-05-16 03:31

Use grep. Sed is an editor. If you only want to match a regexp, grep is more than sufficient.

查看更多
趁早两清
6楼-- · 2020-05-16 03:36
echo "TestT100String" | sed 's/[^0-9]*\([0-9]\+\).*/\1/'

echo "TestT100String" | grep -o  '[0-9]\+'

The method you use to put the results in an array depends somewhat on how the actual data is being retrieved. There's not enough information in your question to be able to guide you well. However, here is one method:

index=0
while read -r line
do
    array[index++]=$(echo "$line" | grep -o  '[0-9]\+')
done < filename

Here's another way:

array=($(grep -o '[0-9]\+' filename))
查看更多
放荡不羁爱自由
7楼-- · 2020-05-16 03:42

I Know this is an old topic but I came her along same searches and found another great possibility apply a regex on a String/Variable using grep:

# Simple
$(echo "TestT100String" | grep -Po "[0-9]{3}")
# More complex using lookaround
$(echo "TestT100String" | grep -Po "(?i)TestT\K[0-9]{3}(?=String)")

With using lookaround capabilities search expressions can be extended for better matching. Where (?i) indicates the Pattern before the searched Pattern (lookahead), \K indicates the actual search pattern and (?=) contains the pattern after the search (lookbehind).

https://www.regular-expressions.info/lookaround.html

The given example matches the same as the PCRE regex TestT([0-9]{3})String

查看更多
登录 后发表回答