C# .NET - Need help in Regular Expression. - Asked By shah zeb on 21-Nov-11 12:43 AM

Need replace text in between two Delimeters. (! and #)
str ="Forum ! blahlksdjflskdjflsk ! dflsldjflskjdflksjdlfjlsdkjflskjdflskdjflskjd #.
I need to replace Text with empty string.
the result of above str should be  "Forum".

Suchit shah replied to shah zeb on 21-Nov-11 12:51 AM
Try :


(/[^ \t/<>]*?)ou([^ \t/<>]*?/)

if having any query let me know
Reena Jain replied to shah zeb on 21-Nov-11 01:24 AM

just use trim function like this

str="Forum ! blahlksdjflskdjflsk ! dflsldjflskjdflksjdlfjlsdkjflskjdflskdjflskjd #.";
string [] temp = str.ToString().Trim("!");
//temp[0] having the forum
//temp[1] having remain text

Now get the temp [0] value which is having the forum

Hope this will help you
shah zeb replied to Reena Jain on 21-Nov-11 03:11 AM
You have not got my point.
I don't wanted  to split it.
i wanted some thing like
forum EggHead blah1 blah2 blah3 CAFE blah4 blah5.....
the regular expression should make the string as  forum blah4 blah5.....
If in above case i consider EggHead and CAFE as my start and end Delimeters.
I wanted a Regular expression which should extract the string.
Hope u will get the point now
kalpana aparnathi replied to shah zeb on 21-Nov-11 05:08 AM
Try this code:
class TestRegularExpressions
  static void Main()
    string[] sentences =
                str ="Forum ! blahlksdjflskdjflsk ! dflsldjflskjdflksjdlfjlsdkjflskjdflskdjflskjd #.
    string sPattern = "Forum";
    foreach (string s in sentences)
      System.Console.Write("{0,24}", s);
      if (System.Text.RegularExpressions.Regex.IsMatch(s, sPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
        System.Console.WriteLine("  (match for '{0}' found)", sPattern);
Devil Scorpio replied to shah zeb on 21-Nov-11 06:02 AM

Please use the following code to get desired string from given string

using System;
using System.Text.RegularExpressions;

public class Example
   public static void Main()
      string input = "Forum ! blahlksdjflskdjflsk ! dflsldjflskjdflksjdlfjlsdkjflskjdflskdjflskjd #.";
      string pattern = " ! blahlksdjflskdjflsk ! dflsldjflskjdflksjdlfjlsdkjflskjdflskdjflskjd #.";
      string replacement = " ";
      Regex rgx = new Regex(pattern);
      string result = rgx.Replace(input, replacement);

      Console.WriteLine("Original String: {0}", input);
      Console.WriteLine("Replacement String: {0}", result);                             
// The example displays the following output:
//       Original String: Forum ! blahlksdjflskdjflsk ! dflsldjflskjdflksjdlfjlsdkjflskjdflskdjflskjd #.
//       Replacement String: Forum
shah zeb replied to Suchit shah on 21-Nov-11 06:26 AM

Shah Can u explian a little what these expression are doing?


(/[^ \t/<>]*?)ou([^ \t/<>]*?/)
Suchit shah replied to shah zeb on 21-Nov-11 07:33 AM
(/[^ \t/<>]*?)ou([^ \t/<>]*?/)

This will ignore text between / characters if there is a space, tab, angle brackets (< and >) or another forward slash (/) in between them.

  • is preceded by a [ that is not captured (lookbehind);
  • a non-greedy captured group. It's non-greedy to stop at the first ]; and
  • is followed by a ] that is not captured (lookahead).
  • Alternatively you can just capture what's between the square brackets:


    and return the first captured group instead of the entire match.

    shah zeb replied to Suchit shah on 21-Nov-11 07:50 AM
    Actually my delimeters are something else
    Suchit shah replied to shah zeb on 21-Nov-11 08:33 AM
    Then just put your delimeters  in to that regular expression as per your requirement... i explain each for step so put it according to the place
    shah zeb replied to Suchit shah on 21-Nov-11 08:43 AM
    thanks shah for another reply
    but the problem is i got a perfect solution which i posted in my another question.
    The solution you posted also fails when the delimiters are nested. It replaces a wrong part.
    I solved it that way
    string strNew = System.Text.RegularExpressions.Regex.Replace(str , @"!.+?\|", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase  );
    In above case if there is no nesting done it works but if exists then it fails ?
    I need a more perfect RE else i will solve the issues by finding index of delimeters.
    Suchit shah replied to shah zeb on 21-Nov-11 08:51 AM
    Ohhh I see.....

    Thats Cool u got the Perfect solution for that..... and Thank u i also got to know from this :)