1

I'm studying regular expressions and cannot figure out what this caret does exactly. I thought that this caret symbol means 'not equal', but in this query below, I am confused:

SELECT REGEXP_REPLACE('San Antonio', '(^[[:alpha:]]+)', 'CITY') TEST
FROM DUAL;

RESULT:

CITY Antonio

'San' should comply with [:alpha:] so I don't understand what the caret function does here.

Cody
  • 389
  • 3
  • 4
  • 11

2 Answers2

5

Carrat (^) also stands for the beginning of the line (and Dollar ($) for its end).

^Hello$  = the word Hello and nothing more
^Hello.* = something that starts with Hello

The negation functionality is within square brackets:

[^0-9]    = anything that is not a digit  
[^a-zA-Z] = anything that is not an english letter   
David דודו Markovitz
  • 42,900
  • 6
  • 64
  • 88
0

Caret ^ (please note the correct spelling) means "at the beginning of the string", but only when it is the very first character in the matching pattern.

'San' does NOT comply with [:alpha:], because [:alpha:] is a SINGLE alphabetic character. [ ... ] means "matching set" (match exactly ONE SINGLE character of those listed within square brackets). [[:alpha:]] means any single alphabetic character. The + means "one or more" of what precedes it, so 'San' matches [[:alpha:]]+ at the beginning of the string. 'Antonio' also matches, but it is not at the beginning of the string, so it is not replaced. If you didn't have the caret, both words would be replaced with CITY (try it and you will see.)