Assuming
- for test_method, we want to match
TM-<number>
- for revision, we want to match
Rev # <number> (note the spaces around #)
Then here's a solution with REGEXP_SUBSTR:
select
regexp_substr(reported_name, 'TM\-[0-9]+') as test_method_regexsub,
regexp_substr(reported_name, 'Rev # [0-9]+') as revision_regexsub
from test t
And here's another one with REGEXP_REPLACE; we have to use the CASE/REGEXP_LIKE workaround to return an empty string if the regex doesn't match, because REGEXP_REPLACE returns the whole string unchanged if no match is found:
select
(case
when regexp_like(reported_name, '.*(TM\-[0-9]+).*')
then regexp_replace(reported_name, '.*(TM\-[0-9]+).*', '\1')
else ''
end) as test_method_regexrepl,
(case
when regexp_like(reported_name, '.*(Rev # [0-9]+).*')
then regexp_replace(reported_name, '.*(Rev # [0-9]+).*', '\1')
else ''
end) as revision_regexrepl
from test t
The second approach uses a capturing group (Rev # [0-9]+) and replaces the whole string with its contents \1.
2nd UPDATE
Assuming
- everything in front of
using should be ignored
- everything up to an optional
Rev is the test method name
- a revision consists of
Rev # <number>, where the first space is optional
this should work:
select reported_name,
(case
when regexp_like(reported_name, '.* using (.*)( - Rev.*)')
then regexp_replace(reported_name, '.* using (.*)( - Rev.*)', '\1')
when regexp_like(reported_name, '.* using (.*)')
then regexp_replace(reported_name, '.* using (.*)', '\1')
else '' end) as test_method_regexrepl,
(case when regexp_like(reported_name, '.* - (Rev[ ]?# [0-9]+)')
then regexp_replace(reported_name, '.*(Rev[ ]?# [0-9]+)', '\1')
else '' end) as revision_regexrepl
from test t
Explanation:
.* using (.*)( - Rev.*) is our regex for a test method that has a revision. It matches
- an arbitrary string
.*
- the string
using (note the two spaces)
- an arbitrary string
(.*) - we use the parentheses () to capture this part of the match in a group
- the string
- Rev, followed by an arbitrary string; again, we use parentheses to capture the string in a group (although we don't really need that)
If we have a match, we replace the whole string with the first capturing group \1 (this contains the part between using and Rev
.* using (.*) is our fallback for a test method without the revision; it matches
- an arbitrary string
.*
- the string
using (note the two spaces)
- an arbitrary string
(.*) - we use the parentheses () to capture this part of the match in a group
If we have a match, we replace the whole string with the first capturing group \1 (this contains the part between using and Rev
.* - (Rev[ ]?# [0-9]+) is our regex for the revision part. It matches
- an arbitrary string followed by a hyphen surrounded by spaces
.* -
- the word
Rev
- an optional space
[ ]?
- a lattice followed by a space
#
- one or more digits
[0-9]+
and again uses a capturing group (Rev...) for the "interesting" part
If we have a match, we replace the whole string with the first capturing group \1 (this contains the part between Rev and the last digit)
SQL Fiddle