0

I just don't understand, why this matches a whole line:

'([^' || chr ( 10 ) || ']*)(' || chr ( 10 ) || '|$)'

What I understand so far is:

(' || chr ( 10 ) || '|$)

matches the end of a line $ OR a linebreak chr ( 10 )

But why does THIS

([^' || chr ( 10 ) || ']*)

match ANYTHING before my first linebreak?

    with Text as
       (select    'This is my first line'
               || chr ( 10 )
               || 'This is my second line'
               || chr ( 10 )
               || 'This is my third line'
                 as Astr
          from dual)
select regexp_substr ( Astr
                      ,'([^' || chr ( 10 ) || ']*)(' || chr ( 10 ) || '|$)' ) First_Line
  from Text

I just don't get it.

nhahtdh
  • 52,949
  • 15
  • 113
  • 149
bl4ckb0l7
  • 3,497
  • 4
  • 23
  • 32
  • 1
    It looks clear: your expression is `[^\n]*` which means *0 or more characters other than a newline symbol*. `chr(10)` is `\n`. Just go to [regex101.com](http://regex101.com) and see for yourself. – Wiktor Stribiżew Sep 04 '15 at 10:26
  • 1
    The regex basically boils down to `([^\n]*)(\n|$)`. The code is rather hard to read. Not sure if there is any way to specify new line character in Oracle literal string, or in the regex syntax supported by Oracle. – nhahtdh Sep 04 '15 at 10:36
  • 1
    Right. Here is [how it works](https://regex101.com/r/yM1mF2/1). And a [fiddle](http://sqlfiddle.com/#!4/a066c/1). Default number of occurrences in Oracle [`regexp_substr`](http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions131.htm) is `1` *meaning that Oracle searches for the first occurrence of `pattern`*. To get multiple matches, refer to [this SO answer](http://stackoverflow.com/a/17597049/3832970). – Wiktor Stribiżew Sep 04 '15 at 10:56
  • Ok, I misinterpreted the caret "^" as the "beginning of the line" and not as a whole "[^\n]" as "Anything, but a linebreak". Thank you! – bl4ckb0l7 Sep 04 '15 at 11:14

0 Answers0