You're replying to a comment by AltairIV.

AltairIV Permalink
January 14, 2013, 09:47

The regex feature can also extract substrings with the BASH_REMATCH array, bash's implementation of backreferencing.

It's also usually better to store the pattern in a separate variable first, then you don't have to struggle to escape everything. Just be sure NOT to quote it inside the test brackets, or it will be treated as a literal string.

$ str='foo12345bar67890'
$ re='[^0-9]+([0-9]+)[^0-9]+([0-9]+)'

$ [[ $str =~ $re ]] && x=${BASH_REMATCH[1]} y=${BASH_REMATCH[2]}

$ echo "$x/$y
12345/67890

Reply To This Comment

(why do I need your e-mail?)

(Your twitter name, if you have one. (I'm @pkrumins, btw.))

Type the word "antispam": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.