Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Re: regexp_substr occurrences problem

Re: regexp_substr occurrences problem

From: Maxim Demenko <mdemenko_at_gmail.com>
Date: Tue, 19 Sep 2006 23:27:02 +0200
Message-ID: <eepnb7$e03$02$1@news.t-online.com>


Vladimir M. Zakharychev schrieb:
> Maxim Demenko wrote:

>> PhilHibbs schrieb:
>>> So what should I write to get DEF and GHI out of 'ABC|DEF|GHI|JKL',
>>> assuming variable length and number of segments, e.g.
>>> 'A|BC|DEF|GHIJ|KLMNO' should match 'BC', 'DEF', and 'GHIJ'?
>>>
>>> Phil Hibbs.
>>>
>> Not knowing much about your restrictions, for the case you requested
>> simple '\|.+\|' will suffice.
>>
>> Best regards
>>
>> Maxim

>
> Nope, it won't. Since + is greedy, this RE will match |BC|DEF|GHIJ|,
> which is not what the OP wants. If I got him correctly, he wants to
> tokenize a string that uses pipe characters as separators, but he
> doesn't need the leading and the trailing token. So I think the right
> expression would be simply '[^\|]+' and regexp_substr should be called
> with occurence starting at 2 and ending at n-1, where n is the number
> of tokens in the string (n can't be told apriori, but the last token
> can be easily detected as n+1'th occurence will return NULL.)
>
> Regards,
> Vladimir M. Zakharychev
> N-Networks, makers of Dynamic PSP(tm)
> http://www.dynamicpsp.com
>

I think, i see what you mean. However, in my understanding OP wanted the

   leading and trailing pipes (at least, it would be the result of his very first examples, if regexp would behave as he thought). If leading and trailing pipes are not required, i would probably use something like select regexp_replace('ABC|DEF|GHI|JLK|MNO', '([^\|]*\|)(.*)(\|[^\|]*)','\2') from dual;

instead of regexp_substr. The problem with '[^\|]+' is , that changing the occurence, can be obtained any desired token from the string, but not all inner tokens together (of course, i may be wrong interpreting the OP).

Best regards

Maxim Received on Tue Sep 19 2006 - 16:27:02 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US