Click here to Skip to main content
15,881,859 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hi All,

I am trying to get a handle on the RegEx method of Regular Expressions in C#
the code below will do the pattern matching for data in < and >, I just want to know a little more
C#
string regExp = @"(?<=<).+?(?=>)";
          //string regExp = @"(?!!=!).+?(?=!)";
          //char[] delimetersChars = { '\n', '\r' };
          //char[] delimeterCharFurther = { '<', '>' };
          //int Location = 0;
          string FileData;

          FileData = rtbData.Text;
          MatchCollection mc = Regex.Matches(FileData, regExp);
          if (mc.Count > 0)
          {
              string temp = "";
              foreach (Match m in mc)
              {
                  temp += m.Value + "\n";
              }
              rtbFormattedData.Text += temp;
          }

If I try to fiddle the routine so it will look for ! around the data
C#
string regExp = @"(?=!).+?(?=!)";

it will still show the leading ! in the data set even though the data is enclosed by !XXXX!
Just wondering if you can use same character twice in RegEx?

Glenn
Posted

The following regular expression pattern
C#
string regExp =@"(?<=!).+?(?=!)";

can be used to exclude the prefix and suffix. In regular expression pattern ?<= matches prefix but excludes it, in the above code the = was missing.

But the problem is we get the words in between required words as the suffix excluded will become the prefix for the next word i.e. we get test1, middle text, othertext from the following example whereas I think we only require between !!
test1, othertext
The following code can be used for that purpose
C#
string regExp =@"!(.+?)!";
string FileData="String starts here !test1! middle text !othertext! some text";
MatchCollection mc = Regex.Matches(FileData, regExp);
if (mc.Count > 0)
{
    string temp = "";
    foreach (Match m in mc)
    {
        if (m.Groups.Count > 1)
            temp += m.Groups[1].Value + "\n";
    }
    Console.WriteLine (temp);
}
 
Share this answer
 
v2
In this regular expression pattern [ and ] are special characters, so, they are to be preceded by \.
string regExp = @"(?<=\[).+?(?=\]);

In this case since the prefix character is different from the suffix character, the problem of capturing the words in between the required words does not arise.
C#
void Main()
{
    string regExp = @"(?<=\[).+?(?=\])";
    string FileData="String starts here [test1] middle text [othertext] some text";
    MatchCollection mc = Regex.Matches(FileData, regExp);
    if (mc.Count > 0)
    {
        string temp = "";
        foreach (Match m in mc)
        {
            temp += m.Value + "\n";
        }
        Console.WriteLine (temp);
    }
}
 
Share this answer
 
v2
Comments
glennPattonWork3 26-Mar-12 11:49am    
So I needed \ before the [ giving \[, I think I need a more in depth slower explanation than the The-30-Minute-Regex-Tutorial...though.
Glenn
ProEnggSoft 26-Mar-12 11:58am    
Thank you for voting and accepting the answers.
glennPattonWork3 26-Mar-12 12:01pm    
Thanks, got me out of a hole, pass on the good vibes! (I'm not a hippy but the sun is shining and it's nearly home time!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900