1

How to get the whole line up to the second double slash (//)

Example text: "C#//text with any symbols */\ even digits 97878//Other don't need text".

I would get "C#//text with any symbols */\ even digits 97878"

I tried to user ^[^\/]*\/\/[^\/]* but any slash spoils everything.

Thanks!

11
  • 1
    If your regex supports lookaheads then perhaps ^.*?//.*?(?=//)? regex101.com/r/oOCSE5/3 Commented Feb 11, 2020 at 12:04
  • 1
    @Nick Fixed: /^.*?\/\/.*?(?=\/\/)/ (taking into accound that in the question they are backslshed). Commented Feb 11, 2020 at 12:07
  • @extempl definitely if they're using a language that requires / delimiters around the regex. Commented Feb 11, 2020 at 12:09
  • @Nick btw, what are .*? construction for? ? seem to be redundant. Commented Feb 11, 2020 at 12:09
  • 1
    @extempl ? makes .* lazy without it the first .* will match to the second to last // if there is a third (or fourth or ...) // in the string (try removing the ? in my demo link and you'll see what I mean) Commented Feb 11, 2020 at 12:11

3 Answers 3

3

You said:

How to get the whole line up to the second double slash (//)

If that's so, you don't need regular expressions; substr + instr combination is capable of doing it:

SQL> with test (col) as
  2    (select 'C#//text with any symbols */\ even digits 97878//Other don''t need text' from dual)
  3  select substr(col, 1, instr(col, '//', 1, 2) - 1) result
  4  from test;

RESULT
-----------------------------------------------
C#//text with any symbols */\ even digits 97878

SQL>

instr(col, '//', 1, 2) says:

  • search string (col in this example) for double slash //
  • starting from the 1st position in a string
  • and find its 2nd appearance
Sign up to request clarification or add additional context in comments.

Comments

1

@Littlefoot answer using SUBSTR and INSTR is the simple (and best) solution, but if you want to use regex you could use REGEXP_REPLACE to remove everything after and including the second //:

with test (col) as
    (select 'C#//text with any symbols */\ even digits 97878//Other don''t // need text' from dual)
SELECT REGEXP_REPLACE(col, '^(.*?//.*?)//.*$', '\1') result
FROM test

Output:

RESULT
C#//text with any symbols */\ even digits 97878

Demo on dbfiddle (including @Littlefoot answer)

1 Comment

@AlexeiDelezhov no worries - I think you have accepted the correct answer.
0

If you really want to do it with a regular expression you can use

REGEXP_SUBSTR(TEXTVAL, '(.*\/\/.*)\/\/', 1, 1, NULL, 1)

db<>fiddle here

Comments

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.