我有以下代码,我想提取特定网址的所有值,我尝试使用rereplacenocase但没有帮助
http://example.com/1.asp?type=298&companyQ=148&companyQ=150&companyQ=176&companyQ=186&productQ=1072&productQ=1042&productQ=1043
我想以逗号分隔值提取companyQ
的所有数值,我该怎么做?
尝试这样
<cfloop list="#myurl#" index="k">
<cfset getcompanyID = ListAppend(k,'&')>
</cfloop>
答案 0 :(得分:2)
肯定会有更优雅的方式去做,而且绝对不止一种方法。我假设url不是实际的应用程序URL,而是变量(URL范围可以更好地处理)
<cfset myurl="http://example.com/1.asp?type=298&companyQ=148&companyQ=150&companyQ=176&companyQ=186&productQ=1072&productQ=1042&productQ=1043">
<cfset getCompanyID="">
<cfoutput>
<cfloop list="#myurl#" index="i" delimiters="&">
<cfif i contains "companyQ">
<cfset x=ListGetAt(i,2,"=")>
<cfset getCompanyID=ListAppend(getCompanyID,x)>
</cfif>
</cfloop>
</cfoutput>
<cfdump var="#getCompanyID#">
答案 1 :(得分:1)
这是您问题的另一种解决方案。
<cfset myurl="http://example.com/1.asp?type=298&companyQ=148&companyQ=150&companyQ=176&companyQ=186&productQ=1072&productQ=1042&productQ=1043">
<cfset urlstring = listLast(myurl,"?")>
<cfset ids = rereplace(arrayToList(rematch("companyQ=[0-9]*", urlstring)),"companyQ=","","all")>
<cfdump var="#ids#">
答案 2 :(得分:0)
您可以尝试以下内容(任何url参数名称的一般解决方案)。请注意在\b
中使用正则表达式字边界REMatchNoCase()
来阻止捕获acompanyQ
等参数。
<cfset the_param_name = "companyQ" />
<cfset the_url = "http://example.com/1.asp?type=298&companyQ=148&companyQ=150&companyQ=176&companyQ=186&productQ=1072&productQ=1042&productQ=1043" />
<cfset the_match = REMatchNoCase("\b#the_param_name#=[^&]+", the_url) />
<cfset the_value_list = replaceNoCase(arrayToList(test), "#the_param_name#=", "", "all") />
<cfdump var="#the_value_list#" />
如果你想获得幻想,你可以使用Java正则表达式(使用lookbehind来确保你只获得正确的参数值):
<cfset param_pattern = createObject("java", "java.util.regex.Pattern").compile("(?i)(?<=[&?]#the_param_name#=)[^&]+") />
<cfset param_match = param_pattern.matcher(the_url) />
<cfset the_value_list = "" />
<cfloop condition="param_match.find()">
<cfset the_value_list = len(the_value_list) ? the_value_list & ",#param_match.group()#" : "#param_match.group()#" />
</cfloop>
<cfdump var="#the_value_list#" />