在某些条件下无痛地从大字符串中提取子字符串
我想轻松创建一个脚本,该脚本读取名为 'objldn' 的关键字字段,并仅提取有时出现在精确位置的五个连续字符。事实上,在关键字字段'objldn'中存在着各种各样的长字符串,其中有一些带有第三个下划线。在第三个下划线之后,如果它存在,我可以获取连续的 5 个字符。 通过以下几行代码,我实现了我想要的:
def LU = doc['objldn'].value.splitOnToken('_');
return LU[3].substring(0, 5);
但是系统返回错误消息“超出范围”:
请求错误:array_index_out_of_bounds_exception,索引 3 超出范围 “def LU = 中长度 3 的边界 doc['objldn'].value.splitOnToken('_'); ...”(无痛脚本)
在索引模式上执行运行时字段或脚本化字段时出错 返回 LU[3].substring(0, 5); ^---- 这里
可能是因为许多字符串没有第三个下划线或者甚至没有一个,因此我需要首先实现一个 IF 语句来评估是否第三个下划线位于字符串中,只有当它存在时,它才会继续执行 splitOnToken()...但我无法正确执行。您能帮我在脚本中添加 IF 语句吗?
In painless I would like to create a script which reads a keyword field called 'objldn' and extracts only five consecutive characters sometimes present in a precise position. Infact, in the keyword field 'objldn' there are a large variety of long strings among which there are some of them with a third underscore. After the third underscore, if it is present, I can fetch the consecutive 5 chars.
Whith the following lines of code I implement what I want:
def LU = doc['objldn'].value.splitOnToken('_');
return LU[3].substring(0, 5);
But the system returnes an error message "out of bounds":
Request error: array_index_out_of_bounds_exception, Index 3 out of
bounds for length 3 in "def LU =
doc['objldn'].value.splitOnToken('_'); ..." (Painless script)error executing runtime field or scripted field on index pattern
return LU[3].substring(0, 5);
^---- HERE
may be it is due to the fact that many strings do not have the third underscore or do not even have one and therefore I need to implement firstly a IF statement which evaluates if a third underscore is in the string and only if it is present it proceeds to execute splitOnToken()... but I am not able to do it correctly. Can you help me to add the IF statement in the script please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不简单地检查
LU
数组的长度?Why not simply checking the length of the
LU
array?