问题描述
假设一个句子是“How is Kunal”,如何使用RPGLE找到单词Kunal在该句子中的起始位置。
解决方法
你没有说你尝试过什么..
但似乎 %SCAN() 会起作用
dcl-s myString varchar(50);
dcl-s posFound int(5);
myString = 'How is Kunal';
pos = %scan('Kunal':myString);
if pos > 0;
//found it
endif;
,
由于您要查找单词,因此您只想查找前面有空格或字符串开头的文本。或者,甚至可能前面有一个非单词字符。 sql 正则表达式函数 regexp_instr
可以做到这一点。
d text s 256a varying
d i5 s 5i 0
d fx s 10i 0
/free
// use capture groups to return position of the search text,only when preceded
// by whitespace or start of string.
// sql function regexp_instr returns either the start position of the
// search text. Or the position after that pattern.
// The 7th parameter specifies which capture group to return the match position of.
text = 'How is Kunal' ;
exec sql
set :fx :i5 = regexp_instr( :text,'(^|\s+)(Kunal)',1,'',2 ) ;