6 Replies - 1453 Views - Last Post: 09 August 2013 - 08:27 AM Rate Topic: -----

#1 oyyou   User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 196
  • Joined: 26-April 10

Regular Expression.. Maybe!?

Posted 08 August 2013 - 09:21 AM

So I have this string
<Font Style=\"FONT-FAMILY: Calibri;FONT-SIZE: 11pt;COLOR: #000000;\">Underline </Font><Font Style=\"FONT-FAMILY: Calibri;FONT-SIZE: 11pt;COLOR: #ff0000;\">Here</Font>

And would like to replace it with this string
[Colour=000000]Underline [Colour=ff0000]Here

So basically, I'm searching through the string for a colour, noting that somewhere, then finding something that is encapsulated in "><", and then noting that down.
I could do this using a whole bunch of loops and bools, but it's messy, and irritating to hell.

I'm just wondering if regular expression woud be helpful in this situation?
And if some, push me in a direction, please!

Is This A Good Question/Topic? 0
  • +

Replies To: Regular Expression.. Maybe!?

#2 lordofduct   User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2668
  • View blog
  • Posts: 4,786
  • Joined: 24-September 10

Re: Regular Expression.. Maybe!?

Posted 08 August 2013 - 09:25 AM

You could use a regular expression.

Or you could treat it as xml.
Was This Post Helpful? 0
  • +
  • -

#3 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: Regular Expression.. Maybe!?

Posted 08 August 2013 - 03:15 PM

Here is an example of using regex
using System;
using System.Text;
using System.Text.RegularExpressions;

namespace Test {
    class Program {
        static void Main(string[] args) {
            Regex reg = new Regex("<Font.*?COLOR: #(?<color>[0-9a-fA-F]{6,6}).*?>(?<text>.*?)</Font>");
            String text = "<Font Style=\"FONT-FAMILY: Calibri;FONT-SIZE: 11pt;COLOR: #000000;\">Underline </Font><Font Style=\"FONT-FAMILY: Calibri;FONT-SIZE: 11pt;COLOR: #ff0000;\">Here</Font>";

            MatchCollection mc = reg.Matches(text);
            StringBuilder sb = new StringBuilder();
            foreach (Match m in mc) {
                sb.Append(String.Format("[Color={0}]{1}", m.Groups["color"], m.Groups["text"]));
            }
            Console.WriteLine(sb.ToString());
            Console.ReadLine();
        }
    }
}

Was This Post Helpful? 1
  • +
  • -

#4 oyyou   User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 196
  • Joined: 26-April 10

Re: Regular Expression.. Maybe!?

Posted 09 August 2013 - 03:25 AM

View PostMomerath, on 08 August 2013 - 03:15 PM, said:

Here is an example of using regex
using System;
using System.Text;
using System.Text.RegularExpressions;

namespace Test {
    class Program {
        static void Main(string[] args) {
            Regex reg = new Regex("<Font.*?COLOR: #(?<color>[0-9a-fA-F]{6,6}).*?>(?<text>.*?)</Font>");
            String text = "<Font Style=\"FONT-FAMILY: Calibri;FONT-SIZE: 11pt;COLOR: #000000;\">Underline </Font><Font Style=\"FONT-FAMILY: Calibri;FONT-SIZE: 11pt;COLOR: #ff0000;\">Here</Font>";

            MatchCollection mc = reg.Matches(text);
            StringBuilder sb = new StringBuilder();
            foreach (Match m in mc) {
                sb.Append(String.Format("[Color={0}]{1}", m.Groups["color"], m.Groups["text"]));
            }
            Console.WriteLine(sb.ToString());
            Console.ReadLine();
        }
    }
}


That works a mo' fo'in' treat!
I really need to learn this "Regex" malarkey, it seems crazy useful.

Thank you for that.
Was This Post Helpful? 0
  • +
  • -

#5 Charles:)   User is offline

  • D.I.C Regular

Reputation: 149
  • View blog
  • Posts: 359
  • Joined: 26-November 09

Re: Regular Expression.. Maybe!?

Posted 09 August 2013 - 06:21 AM

It is useful, but make sure to use it sparingly, and only when it's definitely the best solution for a problem. Regular expressions quickly get hard to read, which means they can be hard to maintain. If you're working in a team then your team mates probably won't thank you for littering the code with regular expressions.
Was This Post Helpful? 0
  • +
  • -

#6 BlueMelon   User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 187
  • Joined: 27-April 10

Re: Regular Expression.. Maybe!?

Posted 09 August 2013 - 07:23 AM

View PostCharles:), on 09 August 2013 - 01:21 PM, said:

It is useful, but make sure to use it sparingly, and only when it's definitely the best solution for a problem. Regular expressions quickly get hard to read, which means they can be hard to maintain. If you're working in a team then your team mates probably won't thank you for littering the code with regular expressions.


Especially when dealing with HTML.
See this coding horror article, Parsing HTML The Cthulhu Way

Just a quick search for 'parsing html c#' reveals interesting library's and alternatives to regex.

I am one who will use regex on occasion to parse through some HTML, but in most cases that I do, it is a quick script that will only be used once/twice and won't need to be extended/maintained.

This post has been edited by BlueMelon: 09 August 2013 - 07:23 AM

Was This Post Helpful? 1
  • +
  • -

#7 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Regular Expression.. Maybe!?

Posted 09 August 2013 - 08:27 AM

Make sure to read the SO thread that article links to, it really sells insanity really well:

http://stackoverflow...1732454#1732454

But seriously, you could handle this with the XML libraries or the HTMLAgilityPack as well. If this is a simple one-off, then by all means use the regesx solution. But if you're going to have to be doing a lot of this, it might pay off in the long run to learn the HTMLAgilityPack.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1