0

I need to use regexp_substr, but I can't use it properly

I have column (l.id) with numbers, for example:

1234567891123!123  EXPECTED OUTPUT: 1234567891123
123456789112!123   EXPECTED OUTPUT: 123456789112
12345678911!123    EXPECTED OUTPUT: 12345678911
1234567891123!123  EXPECTED OUTPUT: 1234567891123

I want use regexp_substr before the exclamation mark (!)

SELECT REGEXP_SUBSTR(l.id,'[%!]',1,13)  from l.table

is it ok ?

5
  • Column data type? Commented Nov 12, 2018 at 13:27
  • I suppose that with "I want use regexp_substr before the exclamation mark" you mean "I want use regexp_substr in oder to get the substring before the exclamation mark"? Commented Nov 12, 2018 at 13:37
  • By "EXCEPTED" do you mean "EXPECTED"? Commented Nov 12, 2018 at 13:45
  • @mathguy, yep.. edited. Commented Nov 12, 2018 at 13:48
  • FYI - The percent sign matches all characters in SQL, but not in regular expressions. Commented Nov 13, 2018 at 14:32

5 Answers 5

3

You can try using INSTR() and substr()

DEMO

select substr(l.id,1,INSTR(l.id,'!', 1, 1)-1) from dual
Sign up to request clarification or add additional context in comments.

Comments

3

You want to remove the exclamation mark and all following characters it seems. That is simply:

select regexp_replace(id, '!.*', '') from mytable;

2 Comments

@Georgy . . . It is really odd that you insist on using regexp_substr() in the question and choose an answer that doesn't use it. Probably the best solution is fa06's answer that avoids regular expressions entirely -- but you seem to want to know how to do this using regexp_substr() (which is quite feasible).
I'm with Gordon here. I must admit that I didn't even notice that I switched from REGEXP_SUBSTR to REGEXP_REPLACE, as the latter is what came to mind when I saw you wanted to use a regular expression. As you are satisfied with my answer, it shows you had better asked more generally: "How do I get the substring before the exclamation mark. Here is my take with REGEXP_SUBSTR, but it doesn't work and gives me the following result ... instead of the desired ...".
1

Look at it like a delimited string where the bang is the delimiter and you want the first element, even if it is NULL. Make sure to test all possibilities, even the unexpected ones (ALWAYS expect the unexpected)! Here the assumption is if there is no delimiter you'll want what's there.

The regex returns the first element followed by a bang or the end of the line. Note this form of the regex handles a NULL first element.

SQL> with tbl(id, str) as (
      select 1, '1234567891123!123' from dual union all
      select 2, '123456789112!123' from dual union all
      select 3, '12345678911!123' from dual union all
      select 4, '1234567891123!123' from dual union all
      select 5, '!123' from dual union all
      select 6, '123!' from dual union all
      select 7, '' from dual union all
      select 8, '12345' from dual
   )
   select id, regexp_substr(str, '(.*?)(!|$)', 1, 1, NULL, 1)
   from tbl
   order by id;

        ID REGEXP_SUBSTR(STR
---------- -----------------
         1 1234567891123
         2 123456789112
         3 12345678911
         4 1234567891123
         5
         6 123
         7
         8 12345

8 rows selected.

SQL>

Comments

0

If you like to use REGEXP_SUBSTR rather than regexp_replace then you can use

SELECT REGEXP_SUBSTR(l.id,'^\d+')

assuming you have only numbers before !

Comments

0

If I understand correctly, this is the pattern that you want:

SELECT REGEXP_SUBSTR(l.id,'^[^!]+', 1) 
FROM (SELECT '1234567891123!123' as id from dual) l

2 Comments

this can return everything after ! if there is nothing preceding ! in the string.
When someone notices a mistake in your answer, and you correct it based on that comment, it would help future readers to acknowledge it. Otherwise the comment may seem out of place.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.