following code replaces Figure 1....so on with <a href=1>1</a> .....so on
but want to replace this " <a href=1>1</a> " ....... with <a href="#fig1">Figure 1</a>....so on
in single line declaration
please help me

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FileInfo n = new FileInfo(textBox1.Text);

            StringBuilder newFile = new StringBuilder();

            string temp = " ";

            string[] file = File.ReadAllLines(textBox1.Text);

            foreach (string line in file)
            {

                if (line.Contains("Figure 1"))
                {

                    temp = line.Replace("Figure 1", "<a href=1>1</a>");

                    newFile.Append(temp + "\r\n");

                    continue;

                }

                if (line.Contains("Figure 2"))
                {
                    temp = line.Replace("Figure 2", "<a href=2>2</a>");

                    newFile.Append(temp + "\r\n");

                    continue;
                }

                newFile.Append(line + "\r\n");

            }

            File.WriteAllText(@"D:\madhu\test\1.xhtml", newFile.ToString());
        }


    }

Try this: temp = line.Replace("Figure 1", @"<a href=""#fig1"">Figure 1</a>")

on a side note, when you have a problem like this, it helps when you explain what you've tried and what it does wrong.

Edited 3 Years Ago by tinstaafl

If you're looking for a short bit of code that will replace all of the "Figure n" text with matching links, Regex.Replace is your friend.

Do you have much experience with regular exressions?

Input:
Figure 1 Figure 2 Figure 3 Figure 13 Figure 35 Figure 1243

Output:

<a href="#fig1">Figure 1 </a>
<a href="#fig2">Figure 2 </a>
<a href="#fig3">Figure 3 </a>
<a href="#fig13">Figure 13 </a>
<a href="#fig35">Figure 35 </a>
<a href="#fig1243">Figure 1243</a>

Using this code: (the regex expression might require some tweaking...still learning it...)

 string input = "Figure 1 Figure 2 Figure 3 Figure 13 Figure 35 Figure 1243";

 MatchCollection matches = Regex.Matches(input, "Figure\\s\\d+(\\s|$)");
 string[] exp = new string[2];

 foreach (Match m in matches)
 {
     exp = Regex.Split(m.Value, "\\s");
     input = input.Replace(m.Value, string.Format("<a href=\"#{0}\">{1}</a>", exp[0].Substring(0, 3).ToLower() + exp[1], m.Value));

 }


 File.WriteAllText("output.txt", input);   // output
  • probably not the best solution, but might be better than nothing :)

Edited 3 Years Ago by TheApex

probably not the best solution

If you know how to use backreferences, Regex.Replace can do this as a one-liner.

I'm just waiting for OP to chime in to see where to start the explanation.

If you know how to use backreferences, Regex.Replace can do this as a one-liner.

Challenge accepted.
For some reason the idea above (match with regex / replace with loop) always comes first to my mind. Still...gave backreferences a try :)

input = Regex.Replace(input, "Figure\\s(\\d+)(\\s|$)", "<a href=\"#fig$1\">Figure $1</a>"); 

Edited 3 Years Ago by TheApex

Challenge accepted.

:)

"Figure\\s(\\d+)(\\s|$)", "<a href=\"#fig$1\">Figure $1</a>"

I prefer verbatim strings for this, meaning I don't have to type \\, which always looks super weird to me in regexes as they're already symbol soup to begin with. Of course, that also means doubling up double quote characters (quadruple quotes?), but for some reason I find that easier on my eyes.

Also, Figure\\s**+** seems a little more robust; it allows us to catch things like this:

Figure  3

(two spaces)

For some reason the idea above (match with regex / replace with loop) always comes first to my mind

My gut says the loop will not perform as well, mainly because of the multiple string operations... but I don't know what exactly Regex.Replace does under the hood. I expect it's at least somewhat optimized, but it also seems like it could be a bit more involved.

It would be interesting to run each one through a profiler a few thousand times to see what happens.

This article has been dead for over six months. Start a new discussion instead.