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

Reply To This Comment

(why do I need your e-mail?)

(Your twitter handle, if you have one.)

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

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