问题描述:

I am very very very new to C# and ASP.NET development.

What I'd like to do is a find-and-replace for certain words appearing in the body text of a web page. Every time a certain word appears in the body text, I'd like to convert that word into a hyperlink that links to another page on our site.

I have no idea where to even start with this. I've found code for doing find-and-replace in C#, but I haven't found any help for just reading through a document, finding certain strings, and changing them into different strings.

网友答案:

A couple of ways to accomplish this.

string text = "We the People of the United States, in Order to form a more perfect Union, establish Justice, insure domestic Tranquility, provide for the common defence, promote the general Welfare, and secure the Blessings of Liberty to ourselves and our Posterity, do ordain and establish this Constitution for the United States of America.";

string augmentedText = text.Replace("provide", "<a href='#provide'>provide</a>");

You could also use regular expressions to accomplish this.

Here's a sample that converts each word to upper case:

public static string MatchEval(Match m)
{
    return m.ToString().ToUpper();
}

static void Main(string[] args)
{
    string text = "This is some sample text.";

    Console.WriteLine(text);

    string result = Regex.Replace(text, @"\w+", new MatchEvaluator(MatchEval));

    Console.WriteLine(result);
}

Hope this helps...... Good luck!

网友答案:

The best performing job to find words or text in a document is by using Regular Expressions. If you are new to these, I would most certainly recommend you to go through it if you're planning to make your project performant.

You might also want to search the internet for Wiki API's, which will help you build your solution, and you not having to reinvent warm water.

I'm pretty sure the following link will give you a head start to learning regular expressions. Download the expression tester and play with it a bit.

http://www.radsoftware.com.au/articles/regexlearnsyntax.aspx

网友答案:

It looks like what you want to do is make a c# application that opens the file and looks at the source code.

You probably need to use regular expressions to get the best matches for the text you want to replace, you also need to be carefull when writing this to make sure it only replaces whole words so for instance if you wanted to create a link for the word tom that takes you to toms page you wouldnt want this to create the link in the word tomorrow etc.

Basically I think the logic is find words with a space before and after and replace it with the code for the hyperlink.

Regex can be a bit daunting when you first look at it, but once you have your expressions it is a very powerfull way to perform this kind of thing.

网友答案:

if you have some specific words, we generally use some special text like [NAME], [CLASS] to recognize the text, then do the following,

  1. Read the html , aspx file with textreader class.
  2. hold that entire text inside a string and start string .replace("[Name]",@"...") ... will be the required attributes,
  3. re-write the text to some new page with the same extension.
网友答案:

Okay Emily, to help you out on your short deadline a bit:

Read the following article on how to fetch the body html content in code-behind: http://west-wind.com/weblog/posts/481.aspx

Let's assume you have that Render() output stored in a variable named _pageContent

I am not using any Regular Expressions now, as I don't have the time to think of one properly. You can play around with that a bit yourself. The following link may point you in a direction: Regex to match multiple strings

public static void ChangeWordsToLinks()
{
  Dictionary<string, string> _wordLinkCollection = new Dicationary<string, string>();
  // fill the collection which will replace words by links here
  // Additionally you can fetch this from a database and loop 
  // through a DataTable to fill this collection
  _wordLinkCollection.add("foo", "http://www.foobar.com");
  _wordLinkCollection.add("bar", "http://www.barfoo.com");

  // this is lazy code and SHOULD be optimized to a single RegExp string.
  foreach(KayValuePair<string, string> pair in _wordLinkCollection)
  {
    _pageContent.Replace(String.Format(" {0} ", pair.Key), 
        String.Format("<a href='{0}'>{1}</a>", pair.Value, pair.Key));
  }
}

Glad if I could be of any help to you

相关阅读:
Top