tags:

views:

47

answers:

1

Hi

Is there a way to replace the last digit with a hyphen and a digit.

original            needs to be replaced with
file01              file01-01
file02              file02-02

With AmarGhosh I solved this

with names as
(
select '/file01/some/file01' name from dual
union all
select '/file02/another/file02' name from dual
)
select name,regexp_replace(name, '^/file0([1-9]+)','/file0\1-\1') new

NAME                      NEW
------------------------- -------------------------
/file01/some/file01       /file01-1/some/file01
/file02/another/file02    /file02-2/another/file02

Thanks Thanks

+3  A: 

Replace ^/file0([12])/ with /file0\1-\1/

Update: for any number of digits following file:

Replace ^/file([0-9]+)/ with /file\1-\1/

Update-2: for any number of /file-01

Replace /file([0-9]+) with /file\1-\1

The ^ matches the beginning of a line and hence your regex doesn't match the second file01 - just get rid of it.

Amarghosh
though from the example, I think the parens should go around the 0 as well:file(0[12])
bluesmoon
Sorry i had to rephrase the question i want the second row to be /file02/another/file02 /file02-2/another/file02
Lisa
@bluesmoon yep, updated. Also changed `$1` to `\1`
Amarghosh
Bluesmoon thanks for your observation
Lisa