如何在 RPGLE 中找到句子中特定单词的起始位置
How to find the starting position of a specific word in a sentence in RPGLE
假设一个句子是“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, 1,
0, '', 2 ) ;
假设一个句子是“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, 1,
0, '', 2 ) ;