Search in Strings
There are 3 ways to search in strings:
1. To search whether a string contains another string, use functions like and ilike . like
is case sensitive and ilike
is case insensitive.
$ a=`IBM`ibm`MSFT`Goog`YHOO`ORCL;
$ a ilike "%OO%";
[0,0,0,1,1,0]
$ a like "%oo%"
[0,0,0,1,0,0]
2. To find the location of the first occurrence of a string in another string, use function strpos .
$ strpos("abcdefg","cd");
2
$ strpos("abcdefg","d");
3
$ strpos("abcdefg","ah");
-1
3. To find the location of the first occurrence of a string or a regular expression pattern, use function regexFind. Compared with function strpos, in regexFind
we can search a regular expression pattern, and we can also specify the starting position to search. To search a string in another string without specifying the starting position to search, we should use strpos
instead of regexFind
as the former is faster.
$ regexFind("FB IBM FB IBM AMZN", `IBM, 7);
10
$ regexFind("this subject has a submarine as subsequence", "\\b(sub)([^ ]*)");
5
$ regexFind("this subject has a submarine as subsequence", "\\b(sub)([^ ]*)", 10);
19