Home » SQL & PL/SQL » SQL & PL/SQL » Pattern Marching using regexp (UNIX, Oracle 11g)
Pattern Marching using regexp [message #628091] Thu, 20 November 2014 02:42 Go to next message
a_oracle
Messages: 98
Registered: November 2010
Member
Hi All,

I have a requirement to match patterns and display the nearest matching record. Will have a test file name as stated in the "file_nm" view and have to get the
nearest matching record from the view inl_query.
the matching logic is:
1. If the patter "test_csv" or "test_txt" is available then its a match.
2. If step 1 holds good then match the patter "region_1" or "region2".
3. If both 1 and 2 hold good, display the corresponding column 'a' from the view inl_query.

for example the query should return me "test_csv_2014-05-20_0800_region_1.csv" from the inl_query as the nearest matching record:

with file_nm as (
select 'test_csv_2013-11-30_0800_region_1_080012678.csv' test_fl 
 from dual),
inl_query as
(select 'test_csv_2014-05-20_0800_region_1.csv' a
   from dual
  union all
 select 'test_csv_2014-05-20_0800_region_2.csv' a
   from dual
  union all
 select 'test_txt_2014-05-20_0800-region_2.csv' 
   from dual)
select a from inl_query
where <regexp conditions>

Re: Pattern Marching using regexp [message #628094 is a reply to message #628091] Thu, 20 November 2014 03:05 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Do 1, 2 and 3 depend on the content of file_nm? If so how?

[Updated on: Thu, 20 November 2014 03:05]

Report message to a moderator

Re: Pattern Marching using regexp [message #628097 is a reply to message #628094] Thu, 20 November 2014 03:14 Go to previous messageGo to next message
a_oracle
Messages: 98
Registered: November 2010
Member
Hi Michel,

The conditions mentioned in 1,2 are treated as standards naming conventions in the application. Considering the above, the test_fl will always having the either "test_csv" or "test_txt" and will also always have either "region_1" or "region_2" in the names. Though maybe the positions may differ.
And also, the columns in inl_query will also follow the same logic.
The only difference maybe the positions of the standard strings.
Please let me know if this is what you had asked for.
Re: Pattern Marching using regexp [message #628100 is a reply to message #628097] Thu, 20 November 2014 03:20 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

So you don't need any regexp, just order your rows as you want and retrieve the one (or those) with rank 1.
For instance:
SQL> with inl_query as
  2  (select 'test_csv_2014-05-20_0800_region_1.csv' a
  3     from dual
  4    union all
  5   select 'test_csv_2014-05-20_0800_region_2.csv' a
  6     from dual
  7    union all
  8   select 'test_txt_2014-05-20_0800-region_2.csv' 
  9     from dual),
 10    inl_query_ranked as (
 11      select a,
 12             rank() over (
 13               order by 
 14                 case when a like 'test_csv%' then 1
 15                      when a like 'test_txt%' then 2
 16                      else 3
 17                 end,
 18                 case when a like '%region_1.csv' then 1
 19                      when a like '%region_2.csv' then 2
 20                      else 3
 21                 end
 22             ) rk
 23      from inl_query
 24    )
 25  select a from inl_query_ranked where rk = 1
 26  /
A
-------------------------------------
test_csv_2014-05-20_0800_region_1.csv

1 row selected.

Re: Pattern Marching using regexp [message #628113 is a reply to message #628100] Thu, 20 November 2014 04:05 Go to previous messageGo to next message
a_oracle
Messages: 98
Registered: November 2010
Member
Thanks Michel..This works perfectly for my requirement.
But just wanted to know if this can be achieved with regexp_instr also. As per I know, multiple string searches in single instr command is not not possible but does regexp_instr handles the same?
Re: Pattern Marching using regexp [message #628126 is a reply to message #628113] Thu, 20 November 2014 05:05 Go to previous message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Why do you want to (try to) use an appropriate tool when you have a correct one?
I don't know if it is possible and I don't care and will surely not waste my time to try to find a bad solution.
Note that ANYWAY you will have to order the possible rows to find the more appropriate one.

Previous Topic: materialized view log size doesn't correlate with its rows quantity
Next Topic: retrieving previous status code
Goto Forum:
  


Current Time: Thu Apr 25 08:20:57 CDT 2024