简体   繁体   English

查找/替换单词拼图

[英]Find/Replace Word Puzzle

Let's say I have an object that contains a Word property and a Sentence property. 假设我有一个包含Word属性和Sentence属性的对象。 The sentence must use the word, and I want to replace the word in that sentence with a link using a public function (say, GetLinkedSentence). 该句子必须使用单词,我想使用公共函数(例如,GetLinkedSentence)将其替换为链接中的单词。 The catch is maybe the sentence uses a plural form of the word and maybe the "word" itself is actually a phrase, or is even hyphenated. 问题是,句子可能使用单词的复数形式,并且“单词”本身实际上是短语,甚至连字符。 There's no length limit to the word either. 这个词也没有长度限制。 How do I find and replace this word (in ASP.NET, preferably VB) with a link (www.example.com/?[word])? 如何找到该单词(在ASP.NET中,最好是VB)并将其替换为链接(www.example.com/?[word])?

Look at RegEx.Replace in the System.Text.RegularExpressions namespace. 查看System.Text.RegularExpressions命名空间中的RegEx.Replace。

For example, to replace the word "weather" in the sentence "The weather today is rain." 例如,在句子“今天的天气是雨”中替换“天气”一词。 with a link, you could do something like the following: 通过链接,您可以执行以下操作:

RegEx.Replace(sSentence, "(weather('s)?)", "<a href='http://www.weather.com'>$1</a>")

The above regex will also replace simple pluralized words (with 's on the end). 上面的正则表达式还将替换简单的复数词(末尾带有)。 You could get more complex expressions for "ies" plurals. 对于“ ies”复数,您可以获得更复杂的表达式。

check out this regex (with help from Expresso, thanks eidylon!): 查看此正则表达式(在Expresso的帮助下,感谢eidylon!):

(?:(?:\\b\\w+)?{1}(?:{2}|{3})?).+?\\b

where: 哪里:

{1} = the whole word minus the last two letters. {1} =整个单词减去最后两个字母。 for example, if the word is "happy", {1} is replaced with "hap" 例如,如果单词为“ happy”,则将{1}替换为“ hap”

{2} = the second to last letter of the word ("p" if the word is "happy") {2} =单词的倒数第二个字母(如果单词“ happy”为“ p”)

{3} = the second to last and last letter of the word ("py" in this example) {3} =单词的倒数第二个(在此示例中为“ py”)

this will find "happy" "happily" "unhappy" "unhappily"... unfortunately for this example, it will also find words like "happened", "hapless", "happening", etc. this will work for me because i'm typically going to be looking for more obscure words where the root is not so common and the word is a bit longer, for instance "exacerbate", so it will be matching "exacerba", "exacerbat" and "exacerbate", which is FAR less common than "hap" "happ" and "happy". 这将找到“快乐”,“快乐”,“不快乐”,“不快乐” ...不幸的是,在此示例中,它还将找到诸如“发生”,“不幸”,“发生”之类的词。这将对我有用,因为我通常,他们会寻找根部不太常见且词长更长的晦涩词,例如“ exacerbate”,因此它将匹配“ exacerba”,“ exacerbat”和“ exacerbate”,从而FAR比“ hap”,“ happ”和“ happy”少见。 it's also important to note it won't find words with extensive root changes, like "person" to "people". 同样重要的是要注意,它不会找到词根变化很大的单词,例如“ person”到“ people”。

so while it's not a PERFECT solution, it works for me :) thanks for pointing me in the right direction, eidylon! 因此,尽管它不是完美的解决方案,但它对我有用:)感谢您为我指明正确的方向,eidylon! now i have a much better understanding of regexes! 现在我对正则表达式有了更好的理解!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM