【问题标题】:Get string with listGetAt delimiter使用 listGetAt 分隔符获取字符串
【发布时间】:2021-09-25 14:48:19
【问题描述】:

我有这个字符串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),它将起作用。请协助我并提前致谢。

中间:https://cffiddle.org/app/file?filepath=ab77d723-1b04-46d6-8d80-fb765b881768/5c8c9eed-a16a-4f3a-b40b-fd7479fdc5ea/dae87d17-2826-4c63-b54c-55a57a8e4398.cfm

【问题讨论】:

标签: string coldfusion delimiter


【解决方案1】:

选项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>   

 

TryCF Examples

更新

这个字符串有问题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'

初始字符串似乎使用name ~|(~ value ~)|~ 格式。但是,如果 value 本身可以包含列表分隔符 ~|(~ 之一,则您将无法使用列表函数,因为该函数无法区分 ~|(~ 何时充当分隔符以及它何时是值的一部分。您需要改用正则表达式。这不是我的强项,但这样的事情应该可以工作

TryCF Example

  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 "~"

【讨论】:

  • 感谢您的时间和榜样。我很感激。谢谢
  • 这个字符串 "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'
  • 这是因为该字符串的格式不同。名字/值的分隔符不正确。应该是supp~|(~lier_isd~)|~。那是错字吗?还有,你是不是故意排除了“id”值'668'
  • 但值本身包含列表分隔符之一~|(~。您将无法使用列表函数,因为该函数无法区分何时充当分隔符和何时充当值的一部分。您需要使用正则表达式。这不是我的强项,但这样的事情应该可以工作trycf.com/gist/2dfca5f01537ed074c66d4a7f9c00603/…
  • 喜欢这个解释!非常感谢
猜你喜欢
  • 2013-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-10
  • 1970-01-01
  • 2022-01-04
相关资源
最近更新 更多