如何在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,'',2 ) ;

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...