There are multiple Teradata Regular Expression Functions which helps to perform variety of String related operation which are not possible using regular string functions. There are five regular expression functions available in Teradata, click on the function below to go directly to that function.
Metacharacter |
Meaning in word |
Description (usefulness) |
Example Pattern |
Comment |
. |
Period |
Wild character which matches any character except newline character |
de.r |
This can match any string with “de” followed by any character and then followed by “r” |
^ |
caret |
Metacharacter which matches start of the String |
^de |
This can match any string starting with “de” and followed by any character string |
$ |
dollar |
Metacharacter which matches end of the String |
ar$ |
This can match any string ending with “ar” and preceded by any character string |
[] |
Square Bracket |
1) This is called character class |
[a-zA-Z] |
[a-zA-Z] can match any string with any alphabet in any case (lower or upper)
|
Metacharacter |
Meaning in word |
Description (usefulness) |
Example |
Comment |
[^] |
Square Bracket/caret |
1) This will invert the characters defined in the class 3)^ should be inside the brackets to invert the character class. 4) ^ has no meaning unless it is the first character in the character class. |
[^A-Z] |
[^A-Z] can match any string not starting with any of the Upper case character |
* |
asterisk |
This means zero or more repetitions of the previous character class or expression. |
[0-9]* |
[0-9]* will match numeric pattern and even will return true if no numeric character is present |
+ |
plus |
This means one or more repetitions of the previous character class or expression. |
[0-9]+ |
[0-9]* will match numeric pattern only if there are 1 or more numeric characters |
? |
question mark |
This means zero or one repetition of the previous character class or expression. |
[0-9]? |
[0-9]* will match pattern only if there is either no numeric or 1 numeric character is present. |
{n,m} |
curley bracket |
This means between n and m repetitions of previous character class or expression. |
6{1,3} |
1) 6{1,3} will match any of the ( 6 or 66 or 666) numeric pattern.
|
Value |
Description |
i |
Matching will be case insensitive. |
c |
Matching will be case sensitive. |
n |
By default Period(.) does not match newline charater and with this parameter, period character can match the newline character as well. |
m |
If source string has new lines characters then '^' and '$' characters apply to each line in source string instead of the entire source string. |
x |
Matching will ignore whitespace. |
l |
Matching will return NULL instead of error if source string exceeds the maximun allowed input size. |
Syntax: REGEXP_SUBSTR(source string, regular expression string [,start from position number, nth occurrence, match argument ])
Note: This function supports 2, 3, 4, or 5 parameters.
SELECT regexp_substr('tractor,scooter,bike,','[s][a-z]+');
Output: scooter
SELECT regexp_substr('tractor,scooter,bike,scooty','[s][a-z]+',15);
Output: scooty
1st occurrence
SELECT regexp_substr('tractor,scooter,bike','[s][a-z]+',1,1);
Output: scooter
2nd occurrence
SELECT regexp_substr('tractor,scooter,bike,scooty','[s][a-z]+',1,2);
Output: scooty
SELECT regexp_substr('tractor,scooter,bike,SCOOTY','[s][a-z]+',1,2,'mi');
Output: SCOOTY
SELECT regexp_substr('tractor scooter bike','[^ ]+',1,1);
Output: tractor
Syntax: REGEXP_REPLACE(source string, regular expression string [,replace string, start from position number, nth occurrence, match argument ])
Note: This function supports 2, 3, 4, 5 or 6 parameters.
SELECT regexp_replace('tractor,scooter,bike','[s][a-z]+');
Output: tractor,,bike
SELECT regexp_replace('tractor,scooter,bike,summer','[s][a-z]+','scooty');
Output: tractor,scooty,bike,scooty
SELECT regexp_replace('tractor,scooter,bike,summer','[s][a-z]+','scooty',15);
Output: tractor,scooter,bike,scooty
2nd occurrence
SELECT regexp_replace('tractor,scooter,bike,summer','[s][a-z]+','scooty',1,2);
Output: tractor,scooter,bike,scooty
SELECT regexp_replace('tractor,scooter,bike,SCOOTY','[s][a-z]+','scooty',1,2,'mi');
Output: tractor,scooter,bike,scooty
Syntax: REGEXP_INSTR(source string, regular expression String [start from position argument, nth occurrence, return option, match argument ])
Note: This function supports 2, 3, 4, 5 or 6 parameters.
SELECT regexp_instr('tractor,scooter,bike,','[s][a-z]+'); --start position of scooter
Output: 9
SELECT regexp_instr('tractor,scooter,bike,scooty','[s][a-z]+',15); --start position of scooty
Output: 22
2nd occurrence
SELECT regexp_instr('tractor,scooter,bike,scooty,baby','[b][a-z]+',1,2); --start position of baby
Output: 29
SELECT regexp_instr('tractor,scooter,bike,scooty,baby','[b][a-z]+',1,2,1); --end position of baby
Output: 33
SELECT regexp_instr('tractor,scooter,bike,SCOOTY','[s][a-z]+',1,2,1,'mi'); --end position of SCOOTY
Output: 28
Syntax: REGEXP_SIMILAR(source string, regular expression String [, match argument ])
Note: This function supports 2 or 3 parameters.
SELECT user_id,regexp_similar(user_id,'(b|p)eta','i') FROM alphanumeric_test;
Output:
User_Id regexp_similar(User_Id,'(b|p)eta','i')
---------- --------------------------------------
alpha 0
324BETA 0
alpha4532 0
PETA 1
beta 1