Good,
I need help to create a regular expression to just take the name and extension of file of the following directories.
/home/user/work/file1.dbf
/opt/user/file2.dfb
I am trying to create an expression in Oracle12C to only output " file1.dbf
" and " file2.dbf
".
I am currently trying to do the regular expression on the next page and reading the following documentation .
Thanks in advance and I hope I have explained correctly.
You don't need a regular expression to do this. A combination of substr
and instr
would be sufficient.
instr(colname,'/',-1)
gets the last occurrence of /
in the string. And the substring after that position would be the filename as per the data shown.
The filter instr(colname,'/') > 0
restricts the rows which don't have a /
in them.
select substr(colname,instr(colname,'/',-1)+1) as filename
from tablename
where instr(colname,'/') > 0
A regular expression for the same would be
select regexp_substr(colname,'(.*/|^)(.+)$',1,1,null,2) as filename
from tablename
(.*/|^)
- All the characters upto the last /
occurence in the string or the start of the string if there are no /
characters.
(.+)$
- All the characters after the last /
if it exists in the string or the full string if /
doesn't exist.
They are extracted as 2 groups and we are interested in the 2nd group. Hence the argument 2
at the end of regexp_substr
.
An alternative regex approach would be to strip everything up to the last /
:
with demo as
( select '/home/user/work/file1.dbf' as path from dual union all
select '/opt/user/file2.dfb' from dual )
select path
, regexp_replace(path,'^..*/') as filename
from demo;
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.