Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Regular Expression to convert from PHP to C#

Regular Expression to convert from PHP to C#

Scheduled Pinned Locked Moved C#
csharpphpregexhelp
21 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Graham Nimbley

    string regex=@"[\r\n]{1,2}(.+)\#(.*)[\r\n]{1,2}";
    string replace=String.Format("{0}\\1{1}\\2{2}\r",myRightVar,myCenterVar,myLeftVar);

    string output=Regex.Replace(input,regex,replace);

    Haven't got VS in front of me, but this (hopefully) should work. Graham. -- modified at 15:34 Sunday 25th June, 2006

    A Offline
    A Offline
    AngryC
    wrote on last edited by
    #6

    It's almost working... the problem is I'm NOT getting the old left text and the old right text shown. I'm getting \1 and \2. What's wrong? Many thanks.

    L 1 Reply Last reply
    0
    • A AngryC

      It's almost working... the problem is I'm NOT getting the old left text and the old right text shown. I'm getting \1 and \2. What's wrong? Many thanks.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #7

      This is a different approach, maybe it works for you?

      string regex = @"[\r\n]{1,2}(.+)\#(.*)[\r\n]{1,2}";

      Match m = Regex.Match(input, regex);
      if (m.Success)
      {
      string output = String.Format("{0}{1}{2}{3}{4}\r", myRightVar, m.Groups[1], myCenterVar, m.Groups[2], myLeftVar);
      Console.WriteLine(output);
      }

      Probably this might also work, it's way shorter:

      string output2 = Regex.Replace(input, regex, String.Format("{0}$1{1}$2{2}\r", myLeftVar, myCenterVar, myRightVar));

      regards

      A 1 Reply Last reply
      0
      • L Lost User

        This is a different approach, maybe it works for you?

        string regex = @"[\r\n]{1,2}(.+)\#(.*)[\r\n]{1,2}";

        Match m = Regex.Match(input, regex);
        if (m.Success)
        {
        string output = String.Format("{0}{1}{2}{3}{4}\r", myRightVar, m.Groups[1], myCenterVar, m.Groups[2], myLeftVar);
        Console.WriteLine(output);
        }

        Probably this might also work, it's way shorter:

        string output2 = Regex.Replace(input, regex, String.Format("{0}$1{1}$2{2}\r", myLeftVar, myCenterVar, myRightVar));

        regards

        A Offline
        A Offline
        AngryC
        wrote on last edited by
        #8

        Thanks a lot but it didn't work for me :( The string I am parsing contains the following: bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla. toto toto toto # toto toto toto toto toto toto # toto toto toto toto toto toto # toto toto toto toto toto toto # toto toto toto bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla. So I want to add alter the content on the following way: bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla. [myLeftVar]toto toto toto [myCenterVar] toto toto toto[myRightVar] [myLeftVar]toto toto toto [myCenterVar] toto toto toto[myRightVar] [myLeftVar]toto toto toto [myCenterVar] toto toto toto[myRightVar] [myLeftVar]toto toto toto [myCenterVar] toto toto toto[myRightVar] And so on... bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla. Please help.

        G 1 Reply Last reply
        0
        • A AngryC

          Thanks a lot but it didn't work for me :( The string I am parsing contains the following: bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla. toto toto toto # toto toto toto toto toto toto # toto toto toto toto toto toto # toto toto toto toto toto toto # toto toto toto bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla. So I want to add alter the content on the following way: bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla. [myLeftVar]toto toto toto [myCenterVar] toto toto toto[myRightVar] [myLeftVar]toto toto toto [myCenterVar] toto toto toto[myRightVar] [myLeftVar]toto toto toto [myCenterVar] toto toto toto[myRightVar] [myLeftVar]toto toto toto [myCenterVar] toto toto toto[myRightVar] And so on... bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla. Please help.

          G Offline
          G Offline
          Graham Nimbley
          wrote on last edited by
          #9

          This should be it.

          string regex=@"(.+)\#(.+)\r\n";
          string replace=String.Format("{0}$1{1}$2{2}\r\n","Left","Center","Right");

          string output=Regex.Replace(input,regex,replace);

          I've tweaked the regex as I was having problem with it. I've dumped the code I used to test it:

          using System;
          using System.Text.RegularExpressions;

          public class Testy
          {
          public static void Main()
          {

          string input="bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla.\r\n\r\n"+
          "toto toto toto # toto toto toto\r\n"+
          "toto toto toto # toto toto toto\r\n"+
          "toto toto toto # toto toto toto\r\n"+
          "toto toto toto # toto toto toto\r\n\r\n"+
          "bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla";

          	string regex=@"(.+)\\#(.+)\\r\\n";
          	string replace=String.Format("{0}$1{1}$2{2}\\r\\n","Left","Center","Right");
          
          	string output=Regex.Replace(input,regex,replace);
          	
          	Console.WriteLine(output);
          }
          

          }

          A 1 Reply Last reply
          0
          • G Graham Nimbley

            This should be it.

            string regex=@"(.+)\#(.+)\r\n";
            string replace=String.Format("{0}$1{1}$2{2}\r\n","Left","Center","Right");

            string output=Regex.Replace(input,regex,replace);

            I've tweaked the regex as I was having problem with it. I've dumped the code I used to test it:

            using System;
            using System.Text.RegularExpressions;

            public class Testy
            {
            public static void Main()
            {

            string input="bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla.\r\n\r\n"+
            "toto toto toto # toto toto toto\r\n"+
            "toto toto toto # toto toto toto\r\n"+
            "toto toto toto # toto toto toto\r\n"+
            "toto toto toto # toto toto toto\r\n\r\n"+
            "bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla";

            	string regex=@"(.+)\\#(.+)\\r\\n";
            	string replace=String.Format("{0}$1{1}$2{2}\\r\\n","Left","Center","Right");
            
            	string output=Regex.Replace(input,regex,replace);
            	
            	Console.WriteLine(output);
            }
            

            }

            A Offline
            A Offline
            AngryC
            wrote on last edited by
            #10

            Thanks a lot Graham it worked :)

            G 1 Reply Last reply
            0
            • A AngryC

              Thanks a lot Graham it worked :)

              G Offline
              G Offline
              Graham Nimbley
              wrote on last edited by
              #11

              Good to know. :) Regexs are good fun, but can be a pain in the a**e sometimes!! :rolleyes:

              A 1 Reply Last reply
              0
              • G Graham Nimbley

                Good to know. :) Regexs are good fun, but can be a pain in the a**e sometimes!! :rolleyes:

                A Offline
                A Offline
                AngryC
                wrote on last edited by
                #12

                I have this left :-O $content = preg_replace('#[color=([^-]+)-(.*)[/color]#Uis', '$2', $content); This is simply to parse bbcode color tag, e.g. [color=red]text[/color] Please help.

                G 1 Reply Last reply
                0
                • A AngryC

                  I have this left :-O $content = preg_replace('#[color=([^-]+)-(.*)[/color]#Uis', '$2', $content); This is simply to parse bbcode color tag, e.g. [color=red]text[/color] Please help.

                  G Offline
                  G Offline
                  Graham Nimbley
                  wrote on last edited by
                  #13

                  Hi

                  using System;
                  using System.Text.RegularExpressions;

                  public class Testy
                  {
                  public static void Main()
                  {
                  string input="This is a [color=red]red[/color] color and this is a [color=blue]blue[/color] color.";
                  string regex=@"\[color=[^\]]*?\](.*?)\[/color\]";

                  	string output=Regex.Replace(input,regex,"$1");
                  	Console.WriteLine(output);
                  }
                  

                  }

                  There was a couple of things wrong with the regex. The original was greedy, and is now lazy. Also some escaping of ']' and '[' was needed. Graham -- modified at 14:41 Monday 26th June, 2006

                  A 1 Reply Last reply
                  0
                  • G Graham Nimbley

                    Hi

                    using System;
                    using System.Text.RegularExpressions;

                    public class Testy
                    {
                    public static void Main()
                    {
                    string input="This is a [color=red]red[/color] color and this is a [color=blue]blue[/color] color.";
                    string regex=@"\[color=[^\]]*?\](.*?)\[/color\]";

                    	string output=Regex.Replace(input,regex,"$1");
                    	Console.WriteLine(output);
                    }
                    

                    }

                    There was a couple of things wrong with the regex. The original was greedy, and is now lazy. Also some escaping of ']' and '[' was needed. Graham -- modified at 14:41 Monday 26th June, 2006

                    A Offline
                    A Offline
                    AngryC
                    wrote on last edited by
                    #14

                    Thanks a lot but what if I want to change the brakets type, say: -color=red- text text -color!- How can I parse it to: text text

                    G 1 Reply Last reply
                    0
                    • A AngryC

                      Thanks a lot but what if I want to change the brakets type, say: -color=red- text text -color!- How can I parse it to: text text

                      G Offline
                      G Offline
                      Graham Nimbley
                      wrote on last edited by
                      #15

                      string regex=@"\[color=([^\]]*?)\](.*?)\[/color\]";
                      string replace=@"<color=$1>$2</color>";

                      string output=Regex.Replace(input,regex,replace);

                      If you are intending to place bbcode for a certain number of tags, it might be easier to use a generic regex. -- modified at 17:58 Monday 26th June, 2006

                      A 1 Reply Last reply
                      0
                      • G Graham Nimbley

                        string regex=@"\[color=([^\]]*?)\](.*?)\[/color\]";
                        string replace=@"<color=$1>$2</color>";

                        string output=Regex.Replace(input,regex,replace);

                        If you are intending to place bbcode for a certain number of tags, it might be easier to use a generic regex. -- modified at 17:58 Monday 26th June, 2006

                        A Offline
                        A Offline
                        AngryC
                        wrote on last edited by
                        #16

                        Many thanks, It worked, but not when it's nested: text text text text [color=red]hello [color=green]ok[/color] test ok[/color] Is there an easy solution for this?

                        G 1 Reply Last reply
                        0
                        • A AngryC

                          Many thanks, It worked, but not when it's nested: text text text text [color=red]hello [color=green]ok[/color] test ok[/color] Is there an easy solution for this?

                          G Offline
                          G Offline
                          Graham Nimbley
                          wrote on last edited by
                          #17

                          The easy way would be to treat the opening and closing tags independently of each other.

                          using System;
                          using System.Text.RegularExpressions;

                          public class Testy
                          {
                          public static void Main()
                          {
                          string input="This is a [color=red]red sentence with [color=blue]some blue[/color] words[/color].";

                          	// Replace open color tag
                          	input=Regex.Replace(input,@"\\\[color=(\[^\\\]\]\*?)\\\]","<color=$1>");
                          	
                          	// Replace close color tag
                          	input=Regex.Replace(input,@"\\\[/color\\\]","</color>");
                          	
                          	Console.WriteLine(input);
                          }
                          

                          }

                          The hard way would involve some complex regexs. Will try to come up with something. Graham. [Edit] Hmmm. It appears that this might be pretty difficult. The problem is that regexs by definition match up by looking forward. To look for nested tags, requires searching the text in tree-wise fashion. To do this in linear text requires requires bilaterial searching, looking forward from the left at the same time looking backwards from the right. Problem is that regexes are virtually impossible to do proper backwards searching. It may be possible to achieve the same effect with some creative code along side regexs. -- modified at 19:13 Monday 26th June, 2006

                          A 1 Reply Last reply
                          0
                          • G Graham Nimbley

                            The easy way would be to treat the opening and closing tags independently of each other.

                            using System;
                            using System.Text.RegularExpressions;

                            public class Testy
                            {
                            public static void Main()
                            {
                            string input="This is a [color=red]red sentence with [color=blue]some blue[/color] words[/color].";

                            	// Replace open color tag
                            	input=Regex.Replace(input,@"\\\[color=(\[^\\\]\]\*?)\\\]","<color=$1>");
                            	
                            	// Replace close color tag
                            	input=Regex.Replace(input,@"\\\[/color\\\]","</color>");
                            	
                            	Console.WriteLine(input);
                            }
                            

                            }

                            The hard way would involve some complex regexs. Will try to come up with something. Graham. [Edit] Hmmm. It appears that this might be pretty difficult. The problem is that regexs by definition match up by looking forward. To look for nested tags, requires searching the text in tree-wise fashion. To do this in linear text requires requires bilaterial searching, looking forward from the left at the same time looking backwards from the right. Problem is that regexes are virtually impossible to do proper backwards searching. It may be possible to achieve the same effect with some creative code along side regexs. -- modified at 19:13 Monday 26th June, 2006

                            A Offline
                            A Offline
                            AngryC
                            wrote on last edited by
                            #18

                            Worked, again :) Which one is better for the closing tab in your opinion: This: output = input.Replace("[/color]", ""); Or this: input=Regex.Replace(input,@"\[/color\]",""); The first one is faster right? I PHP normal function is faster than regular expression, is it the samething in C#? -- modified at 19:16 Monday 26th June, 2006

                            G 1 Reply Last reply
                            0
                            • A AngryC

                              Worked, again :) Which one is better for the closing tab in your opinion: This: output = input.Replace("[/color]", ""); Or this: input=Regex.Replace(input,@"\[/color\]",""); The first one is faster right? I PHP normal function is faster than regular expression, is it the samething in C#? -- modified at 19:16 Monday 26th June, 2006

                              G Offline
                              G Offline
                              Graham Nimbley
                              wrote on last edited by
                              #19

                              They both do the same job. The first it a straight substitution, the second a regex. The first would be marginally faster to perform. Have you read my edit on the harder solution?

                              A 1 Reply Last reply
                              0
                              • G Graham Nimbley

                                They both do the same job. The first it a straight substitution, the second a regex. The first would be marginally faster to perform. Have you read my edit on the harder solution?

                                A Offline
                                A Offline
                                AngryC
                                wrote on last edited by
                                #20

                                I just did. Anyway, the easy method is more than enough for me. I really appreciate your help.

                                G 1 Reply Last reply
                                0
                                • A AngryC

                                  I just did. Anyway, the easy method is more than enough for me. I really appreciate your help.

                                  G Offline
                                  G Offline
                                  Graham Nimbley
                                  wrote on last edited by
                                  #21

                                  That's okay. That's my alotment of regexs used up for the night! :cool:

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups