简体   繁体   中英

Find/Replace Word Puzzle

Let's say I have an object that contains a Word property and a Sentence property. 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). 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])?

Look at RegEx.Replace in the System.Text.RegularExpressions namespace.

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.

check out this regex (with help from Expresso, thanks eidylon!):

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

where:

{1} = the whole word minus the last two letters. for example, if the word is "happy", {1} is replaced with "hap"

{2} = the second to last letter of the word ("p" if the word is "happy")

{3} = the second to last and last letter of the word ("py" in this example)

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". it's also important to note it won't find words with extensive root changes, like "person" to "people".

so while it's not a PERFECT solution, it works for me :) thanks for pointing me in the right direction, eidylon! now i have a much better understanding of regexes!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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