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

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

问题描述

技术细节

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

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 = "一些随机字符串";

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

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

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

我认为会有类似正则表达式的东西,可以提取 2 个值 request.config.${1} = ${2}; 并检索 ${1} 和 ${2},对于每一行,但前提是它匹配.

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!

一些 META:我想要做什么

我正在构建一个 Web 应用程序,它允许最终用户修改一些存储在 params_file.cfm 中的应用程序参数.我们希望能够在应用程序中完成,而不是让开发人员手动更改文件中的变量.

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.

我的应用程序首先对后端进行 AJAX 调用,它读取 params 文件,获取所有数据对(param_name、param_value 以及可能稍后在 param_description 上)并将它们作为 JSON 返回以填充我的列表自动完成工具按名称搜索它们(好奇的 Typeahead.js).当我选择一个参数名称时,该值与一些用于修改它们的控件一起出现(控件取决于数据类型,JQuery 用于确定类型).

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).

问题是 param_value 可以有多种形式.因为这个 params 文件由不同的人维护,它可以有不同的语法.例如,布尔值可以存储为真"、真"、真、真(你明白了).

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).

由于 SerializeJSON 处理类型(布尔值、数字、字符串),我认为我的 REGEX 应该返回不带引号的文本(单引号或双引号),但我在制作该表达式时遇到了麻烦.

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.

我明白了

我用 request.config.my_param_1 = 'MYTEST123'; 对其进行了测试,它仅删除第一个单引号,由于某种原因,表达式返回了我 MYTEST123' 当我不想要任何周围的报价时.我的正则表达式需要帮助

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

推荐答案

你不想要 REmatch,你想要 REFind (docs):

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

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

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 将是一个带有两个键(POSLEN)的结构,列出每个子匹配的位置和长度.

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

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

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

别忘了检查 REFind 是否成功,ArrayLen(match.POS) 在你的情况下必须是 3 (1 个整体匹配,两个匹配组,想想 <代码>$0 .. $2).

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

  • 在循环中运行此函数,将 start 设置为 match.POS[1] + match.LEN[1] 以进行下一次迭代
  • 或逐行循环文件,通过 以换行符 Chr(10) 作为分隔符或通过 <cfloop 数组>ArrayToList(file, Chr(10)).
  • 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天全站免登陆