string - 使用 listGetAt 分隔符获取字符串
问题描述
我有这个字符串supplier_id ~|(~ '3422' ~)|~supplier_name ~|(~ 'WD Ltd.' ~)|~project_personnel ~|(~ 'Yaya Toure (temp)' ~)|~lt_project_code ~|(~ '013-7718321' ~)|~ id ~|(~ '668'
我需要在里面获取内容~|(~data~)|~
。我使用listGetAt
分隔符。(
问题是分隔符内的某些数据包含破坏搜索的括号。例如Yaya Toure (temp)
,包含括号。
<cfoutput>
<cfset test = "supplier_id ~|(~ '3422' ~)|~supplier_name ~|(~ 'WD Ltd.' ~)|~project_personnel ~|(~ 'Yaya Toure (temp)' ~)|~lt_project_code ~|(~ '013-7718321' ~)|~ id ~|(~ '668'">
#test#
<cfset count_column = ListLen(test, "~)|~")>
<cfset column_name = ''>
<cfloop index="i" from="1" to=8 >
<cfif i mod 2 EQ 0>
<cfset column_name = ListAppend(column_name,listGetAt(test, i, "~|(~~)|~" ),",")>
</cfif>
</cfloop>
<br>
<br>
Result : #column_name#
</cfoutput>
输出 :Result : '3422' , 'WD Ltd.' , 'Yaya Toure ,'
我的预期结果是:'3422' , 'WD Ltd.' , 'Yaya Toure (temp)' , '013-7718321'
。(temp)
如果我从字符串中删除它将起作用。请协助我,并提前致谢。
解决方案
选项 1:使用支持多字符分隔符的 CF 列表函数,例如listToArray。
<cfscript>
// split on first delimiter get "name->value" string
nameValuePairs = test.listToArray("~)|~" , false, true);
columnValues = [];
nameValuePairs.each(function(pair, index) {
// split on second delimiter to extract value
local.data = pair.listToArray("~|(~", false, true);
columnValues.append( local.data[ 2 ] );
});
writeDump( "Result 1: "& columnValues.toList() );
</cfscript>
选项 2:如果您可以识别值中从未存在的单个字符,可能是不可打印的字符,只需替换现有的分隔符,然后像往常一样循环。
<cfscript>
// 31 == Unit separator
delim = chr(31);
newString = test.replace( "~)|~", delim, "all")
.replace( "~|(~", delim, "all");
columnValues = [];
newString.listToArray( delim ).each(function( elem, index) {
if (index mod 2 == 0) {
columnValues.append( elem );
}
});
WriteOutput( "<br>Result 2: "& columnValues.toList() );
</cfscript>
更新
supp~|(~lier_isd ~|(~ '3422' ~)|~supplier_name ~|(~ 'WD Ltd.' ~)|~project_personnel ~|(~ 'Yaya Toure (temp)' ~)|~lt_project_code ~|(~ '013-7718321' ~)|~ id ~|(~ '668'
虽然我期待的这个字符串有问题lier_isd ~|(~ '3422', 'WD Ltd.', 'Yaya Toure (temp)', '013-7718321'
最初的字符串似乎使用了 format name ~|(~ value ~)|~
。但是,如果值本身可以包含列表分隔符之一 ~|(~
,您将无法使用列表函数,因为该函数无法区分何时~|(~
充当分隔符和何时充当值的一部分。您需要改用正则表达式。这不是我的强项,但这样的事情应该有效
values = [];
// Find groups of "~|(~ value ~)|~"
matches = reMatch( "~\|\(~(.+?)(?=~\)\|~)", test);
matches.each( function(item, index) {
// remove text up to and including the first "~|(~"
local.str = reReplace( item, ".*?~\|\(~", "");
values.append( local.str );
});
WriteOutput( "Result 1: "& values.toList() );
解释
~ Find tilde "~"
\| Find pipe "|"
\( Find open parenthesis "("
~ Find tilde "~"
(.+?) One ore more characters, non-greedy
(?= Followed by (Non-capturing lookahead)
~ tilde "~"
\) close parenthesis "("
\| pipe "|"
~ tilde "~"
) End lookahead
.*? Zero or more characters
~ Find tilde "~"
\| Find pipe "|"
\( Find open parenthesis "("
~ Find tilde "~"
推荐阅读
- regex - 如何将两个 Windows ascii 文本文件与 cmd for 循环结合起来?
- awk - awk 在脚本中设置命令行选项
- testing - 如何从命令行运行 test cafe studo 测试
- c# - 如何更新除特定值之外的枚举值
- asp.net-core - asp.net 核心在负载均衡器后面运行的应用程序之间共享远程或使用公共 wwwroot
- node.js - 反应/快递之间的CORS标头问题
- ios - 将数据加载到 UITableView 中,没有数据显示?
- angular - 如何在角度的ng-multiselect-dropdown中添加自定义按钮
- video-streaming - 为什么这个 GStreamer 管道可以与 filesink 一起使用,但不能与 ximagesink/autovideosink 一起使用?
- sql - 如何在表格中找到价值的百分比?