1

我试图弄清楚如何只显示 | 之间的单词 和 | 在下面的 XML 描述中。例如,单词“web”或“VideoGames”。

我已经尝试过了,但是因为每个单词的长度不同,所以我不成功。另外,我无法摆脱 |

<cfoutput>#Right(thefeed2.rsschannel.eachresult.resultnumber[x].metadata.xmlAttributes.v, 10)#</cfoutput>

我也在尝试做一个相反的任务——过滤掉 | 之间的单词。和 | 所以它不显示。换句话说,以第一项为例,显示整个描述,减去单词“web”或“VideoGames”

我已经尝试过了,但同样,我遇到了与试图过滤掉描述相同的问题,没有中间的词 | 和|。

<cfoutput>#left(thefeed2.rsschannel.eachresult.resultnumber[x].metadata.xmlAttributes.v, 500)#</cfoutput>

所以我的问题是...

1:如何提取|之间的单词 和 | 从描述元素。

2:在与数字 1 不同的实例中,如何删除 | 之间的单词 和 | 在描述中?

顺便说一下,“thefeed2”就是我所说的 XML 提要。

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<rsschannel>
<resultsnumbertotal>11    </resultsnumbertotal>
<eachresult>
<resultnumber N="1">
<U>/file.cfm?id=yahoocom    </U>
<T>Yahoo    </T>
<metadata N="description" V="Quickly find what you're searching for, get in touch with friends and stay in-the-know |Web|"/>
</resultnumber>

<resultnumber N="2">
<U>/file.cfm?id=halo    </U>
<T>Halo    </T>
<metadata N="description" V="Halo is a multi-billion dollar science fiction video game franchise created by Bungie and now managed by 343 Industries and owned by Microsoft Studios. |VideoGames|"/>
</resultnumber>

<resultnumber N="3">
<U>/file.cfm?id=bingcom    </U>
<T>Bing    </T>
<metadata N="description" V="Bing is a search engine that brings together the best of search and people in your social networks to help you spend less time searching and more time doing. |Web|"/>
</resultnumber>

<resultnumber N="4">
<U>/file.cfm?id=lal    </U>
<T>Lakers    </T>
<metadata N="description" V="The Los Angeles Lakers are an American professional basketball team based in Los Angeles, California. They play in the Pacific Division |Sports|"/>
</resultnumber>

<resultnumber N="5">
<U>/file.cfm?id=quick    </U>
<T>Stay in the Know    </T>
<metadata N="description" V="Quickly find what you're searching for, get in touch with friends and stay in-the-know |Misc|"/>
</resultnumber>

<resultnumber N="6">
<U>/file.cfm?id=multi    </U>
<T>Billion Dollars    </T>
<metadata N="description" V="Halo is a multi-billion dollar science fiction video game franchise created by Bungie and now managed by 343 Industries and owned by Microsoft Studios. |Misc|"  />
</resultnumber>

<resultnumber N="7">
<U>/file.cfm?id=searching    </U>
<T>Searches    </T>
<metadata N="description" V="Bing is a search engine that brings together the best of search and people in your social networks to help you spend less time searching and more time doing. |Web|" />
</resultnumber>

<resultnumber N="8">
<U>/file.cfm?id=LosAngeles    </U>
<T>Los Angeles    </T>
<metadata N="description" V="The Los Angeles Lakers are an American professional basketball team based in Los Angeles, California. They play in the Pacific Division |Sports|"/>
</resultnumber>

<resultnumber N="9">
<U>/file.cfm?id=quick    </U>
<T>Stay in the Know    </T>
<metadata N="description" V="Quickly find what you're searching for, get in touch with friends and stay in-the-know |Misc|"/>
</resultnumber>

<resultnumber N="10">
<U>/file.cfm?id=LosAngeles    </U>
<T>Los Angeles    </T>
<metadata N="description" V="The Los Angeles Lakers are an American professional basketball team based in Los Angeles, California. They play in the Pacific Division"/>
</resultnumber>

<resultnumber N="11">
<U>/file.cfm?id=quick    </U>
<T>Stay in the Know    </T>
<metadata N="description" V="Quickly find what you're searching for, get in touch with friends and stay in-the-know |SummaryofDescription|"/>
</resultnumber>

</eachresult>
</rsschannel>
4

3 回答 3

1

您想将正则表达式与 REreplace() 一起使用。示例(在 cfscript 中):

1)

origString = thefeed2.rsschannel.eachresult.resultnumber[x].metadata.xmlAttributes.v;
newString = REreplace(origString, "\|.*\|", "new text");

2)

origString = thefeed2.rsschannel.eachresult.resultnumber[x].metadata.xmlAttributes.v;
newString = REreplace(origString, "\|.*\|", "");

正则表达式\|.*\|将匹配任何以 | 开头和结尾的文本。

于 2012-10-04T00:45:02.520 回答
1

总是只有两个|符号吗?如果是这样,您可以使用它GetToken来查找值

<cfset Mystring = GetToken(variable,1,'|') />

GetToken将您的变量视为带有分隔符的列表|

要删除您可以使用的单词replace(variable,Mystring,'')

于 2012-10-04T00:42:32.960 回答
1

这个怎么样。对于字符串操作,总是后退一步寻找模式来帮助你发挥创造力。例如,CFML 的“列表”概念会带来一些有趣的时刻。更高级的用户总是会直接使用正则表达式,但如果您刚刚开始,这绝对是一个高级主题。立即,我将您的字符串识别为由竖线 (|) 分隔的列表。您还可以看到句子是由空格分隔的单词列表。这是一些代码。

 //set your string to something
 <cfset myString = "thefeed2.rsschannel.eachresult.resultnumber[x].metadata.xmlAttributes.v" />
//now let's treat the phrase as a list, we'll get position 2 of the list.
<cfset myWord = listGetAt(myString,2,'|');
//assuming you don't want the pipes in the clean string, let's just do a fast replace once
<cfset cleanString = replace(myString,"|#myWord#|","") />
//if you do want the pipes then you'll need a regex (a more advanced topic)
<cfset cleanStringWithPipes = rereplace(myString,"(\|)[ A-Za-z0-9]+(\|)","\1\2") />
于 2012-10-04T00:49:30.467 回答