ColdFusion从文本文件中提取值 [英] ColdFusion extract values from text file

查看:129
本文介绍了ColdFusion从文本文件中提取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

技术细节



我想从包含参数名称和值的文本文件中提取值。对于以request.config开头的每一行。 (有空行,有注释的行等,我不想从中提取任何东西)我想提取这些值(粗体):



请求.config。 my_param_1 = 一些随机字符串;



我认为最好的方法是使用REGEX,但是我该怎么办呢?



我认为会有一个像正则表达式那样提取2个值 request.config 。$ {1} = $ {2}; ,并为每一行检索$ {1}和$ {2},但只有匹配。



我尝试尝试,但没有工作:

 < cfset str =request.config.MY_PARAM_NAME = '参数VALUE !!';> 
< cfset arrSearch = rematch(^ request.config。(。*?)=(。*?); $,str)>
< cfdump var =#arrSearch#label =提取的值>

不幸的是,此代码给了我已经有的FULL STRING,提取值



某些META:我要做什么 b

我正在构建一个web应用程序,让最终用户修改存储在params_file.cfm中的一些应用程序参数。



我的应用程序首先对后端进行AJAX调用,而不是让开发人员在文件中手动更改变量。 ,它读取params文件,获取所有的数据对(param_name,param_value,可能在一个param_description),并返回它们作为JSON填充我的列表,我有一个自动完成工具来搜索他们的名称(Typeahead.js为好奇)。当我选择一个参数名称时,该值会和一些控件一起修改(控件取决于数据类型,JQuery用于确定类型)。



事情是param_value可以采取多种形式..因为这个params文件是由不同的人维护,它可以有不同的语法。例如,布尔值可以存储为TRUE,true,TRUE,true(你会得到想法)。



因为SerializeJSON处理类型(布尔,数字,字符串)我认为我的REGEX应该返回我的文本没有引号(单或双),但我有麻烦



我有



< cfset match = REFind ^ request\.config \。(\S +)= ['|]?(。*)['|]?; $,str,1,Yes)& c $ c>



我用 request.config.my_param_1 ='MYTEST123'; strong> ONLY REMOVES THE FIRST SINGLE QUOTE ,由于某种原因,当我不想要任何周边报价时,表达式会返回 MYTEST123'。我需要帮助我的REGEX

解决方案

你不想要 REMatch ,您需要 REFind docs < a>):

  REFind(reg_expression,string [,start,returnsubexpressions])



returnsubexpressions 是您需要的,所以...

 < cfset str =request.config.MY_PARAM_NAME ='参数VALUE!';> 
< cfset match = REFind(^ request\.config \。(\S +)=(。*);,str,1,Yes)&

< cfdump var =#match#>

match POS LEN ),列出每个子匹配的位置和长度。



然后,您可以将此信息提供给 Mid(),并剪下实际的子字符串。



不要忘记检查 REFind 是否成功, ArrayLen(match.POS) 在您的情况下必须为3(1个总体匹配,两个匹配组,认为 $ 0 .. $ 2 )。



要查找整个文件中的所有匹配项,




  • 在一个循环中运行此函数,将 start 设置为 match.POS [1] + match.LEN [1] 用于下一次迭代

  • 或通过< cfloop list> 以逐行方式循环遍历文件newline Chr(10)作为分隔符或通过< cfloop array> ArrayToList ,Chr(10))


The technical details

I want to EXTRACT values from a text file containing parameter names and values. For each line that starts with "request.config." (there are empty lines, lines with comments, etc. which I don't want to extract anything from) I want to extract these values (in bold) :

request.config.my_param_1 = "some random string";

I thought the best way to do this might be using REGEX, but how can I do this?

I thought there would be something like a regular expression that would extract the 2 values request.config.${1} = ${2}; and retrieve ${1} and ${2}, for each line, but only if it matches.

I tried experimenting but it did not work:

<cfset str = "request.config.MY_PARAM_NAME = 'The parameter VALUE!!';">
<cfset arrSearch = rematch("^request.config.(.*?) = (.*?);$", str) >
<cfdump var="#arrSearch#" label="Extracted values">

Unfortunately, this code gives me the FULL STRING I already had, I just want the 2 extracted values!

Some META : WHAT I'm trying to do

I am building a web app that lets end-users modify some application parameters which are stored in a params_file.cfm. Instead of having developpers change the variables manually in the file, we want to be able to do it from within the application.

My application first makes an AJAX call to the backend, which reads the params file, get all the data pairs (param_name, param_value and possibly later on a param_description) and returns them as JSON to populate my list for which I have an autocomplete tool to search them by name (Typeahead.js for the curious). When I select a parameter name the value appears along with some controls to modify them (the controls depend on the data type, JQuery is used to determine the type).

The thing is the param_value can take many forms.. because this params file is maintained by different people it can have different syntax. For example a boolean can be stored as "TRUE", 'true', TRUE, true (you get the idea).

Since the SerializeJSON handles the types (booleans, numbers, strings) I thought my REGEX should return me the text WITHOUT the quotes (single or double) but I am having trouble crafting that expression.

I got

<cfset match = REFind("^request\.config\.(\S+) = ['|""]?(.*)['|""]?;$", str, 1, "Yes")>

and I tested it with request.config.my_param_1 = 'MYTEST123'; and it ONLY REMOVES THE FIRST SINGLE QUOTE, for some reason the expression returns me MYTEST123' when I don't want any surrounding quote. I need HELP with my REGEX

解决方案

You don't want REMatch, you want REFind (docs):

REFind(reg_expression, string [, start, returnsubexpressions ] )

returnsubexpressions is what you need, so...

<cfset str = "request.config.MY_PARAM_NAME = 'The parameter VALUE!!';">
<cfset match = REFind("^request\.config\.(\S+) = (.*);", str, 1, "Yes")>

<cfdump var="#match#">

match will be a Struct with two keys (POS and LEN), listing the positions and lengths of each sub-match.

You can then feed this information to Mid() and cut out the actual substrings.

Don't forget to check whether REFind succeeded, ArrayLen(match.POS) must be 3 in your case (1 overall match, two match groups, think $0 .. $2).

To find all occurrences in the entire file, either

  • run this function in a loop, setting start to match.POS[1] + match.LEN[1] for the next iteration
  • or loop through the file in a line-by-line manner, via <cfloop list> with newline Chr(10) as delimiter or via <cfloop array> and ArrayToList(file, Chr(10)).

这篇关于ColdFusion从文本文件中提取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆