Regex find nth occurance of hyphen

Why using regex ? This could be done programmatically:

  • explode(PHP) or split by -
  • print 2 last matches

With Perl:

$ perl -ne 'print join "-", +(split(/-/))[-2,-1]' <<< prod-api-mysql-3
mysql-3

With awk:

$ awk 'BEGIN{FS=OFS="-"}{print $(NF-1), $NF}' <<< prod-api-mysql-3
mysql-3

With Python:

>>> "-".join('prod-api-mysql-3'.split('-')[-2:])
'mysql-3'

With Javascript:

> 'prod-api-mysql-3'.split('-').slice(-2).join('-')
'mysql-3'

With PHP:

php -r 'echo(implode("-", array_slice(explode("-", "prod-api-mysql-3"), -2)));'
mysql-3

Read more here: Source link