简体   繁体   中英

Regex to extract values between brackets

I've looked at a few threads on this and just can't seem to get it working. Clearly an issue with my regex statement and/or bash_rematch.

There will only ever be a max of 4 x ()'s

Have the following bash script:

#!/bin/bash
brackets_regex="\((.*?)\)"
text="random date (entry1) some more random data (entry2) random (entry3) random data (entry4)"

if  [[ $text =~ $brackets_regex ]]; then
    echo ${BASH_REMATCH[0]};
    echo ${BASH_REMATCH[1]};
    echo ${BASH_REMATCH[2]};
    echo ${BASH_REMATCH[3]};
fi

Expected output should be:

entry1
entry2
entry3
entry4

Current output:

(entry1) some more random data (entry2) random (entry3) random data (entry4)
entry1) some more random data (entry2) random (entry3) random data (entry4

Using gnu grep:

grep -oP '\(\K[^)]*' <<< "$text"
entry1
entry2
entry3
entry4

Using gnu-awk:

text="random date (entry1) some more random data (entry2) random (entry3) random data (entry4)"
awk -v FPAT='\\([^)]*\\)' '{for(i=1; i<=NF; i++) {gsub(/[()]/, "", $i); print $i}}' <<< "$text"
entry1
entry2
entry3
entry4

Bash regex does not support lazy quantifiers. You need to rely on a negated character class [^()] matching any character but ( and ) .

Here is another way of achieving what you need:

#!/bin/bash
text="random date (entry1) some more random data (entry2) random (entry3) random data (entry4)"
brackets_regex="\(([^()]*)\)"
for s in ${text[@]}; do
    if [[ ${s} =~ $brackets_regex ]]; then
        echo ${BASH_REMATCH[1]};
    fi
done

See IDEONE demo

Output:

entry1
entry2
entry3
entry4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM