views:

255

answers:

2

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])?

+1  A: 

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'&gt;$1&lt;/a&gt;")

The above regex will also replace simple pluralized words (with 's on the end). You could get more complex expressions for "ies" plurals.

eidylon
that sort of helps, but i don't know hardly anything about regexes...
Jason
I hadn't really much experience with them until a few years ago either... but once I found them, WOW! They are SO powerful (though of course, like everything, they have their limits). There are a couple good resources which helped me to learn them. http://regexlib.com/ - this is a great website, with a searchable repository of regexs, which you can either use straight or as starting points to mod and play with. http://www.ultrapico.com/Expresso.htm - Expresso is an awesome little regex building tool, which shows you the grammatical deconstruction of the regex as you build it.
eidylon
expresso did the trick... see my answer for my regex
Jason
A: 

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!

Jason