Hello to everyone,
When I execute this sentence:
select REGEXP_REPLACE('XXX A1 A2 XXX', '.*( )(A[0-9]).*', '\2', 1, 1, 'i');
I get the second "(A[0-9])" expression, that is, "A2", instead of the first one ("A1").
I would like to know why this is like that and how to get "A1".
Thanks in advance and best regards
Thanks yuvaevergreen, but the thing is that I don't know if I'll have two "(A[0-9])" expressions... I could have 3, 4... or just 1.
I wonder why I have to say explicitly that there has to be another "(A[0-9])" expression to make the sentence choose the first one. It's like the regular expression motor follows a right to left order, isn't it?
Even something like:
So isn't there a way to say that I want the first expression (keeping in mind that there could be just 1) that gets the pattern regardless of what I have before and after?
Thanks and regards
Your regex is greedy, you need to change it to '(.*? )(A[0-9])(.*)'
Btw, if you really need to return the first An only why don't you switch to REGEXP_SUBSTR?
SELECT REGEXP_SUBSTR('XX A0 XXX A1 A2 A3 XXX A4 A5', '(A[0-9])', 1, 1, 'i');
IF you are sure of the first qualifier, then the regex mentioned by Deiter will work perfect. But, better option would be to extract the first occurence and then work upon it.
I wouldn't expect much difference, most of the CPU is probably used by the search.
There are some differences between Oracle & Teradata regex, Teradata supports both lookahead (?=...) and lookbehind (?<=...) to add a non-matching group.