正则表达式的疯狂,还是如何使其可变? [英] RegEx madness, or how to make it variable?

查看:50
本文介绍了正则表达式的疯狂,还是如何使其可变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在尝试获取C#中的正则表达式RegEx方法的句柄
下面的代码将对<中的数据进行模式匹配.和> ;,我只是想了解更多信息

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

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

string regExp = @"(?=!).+?(?=!)";


它仍然会显示领先!即使数据用!XXXX括起来,也不会在数据集中显示
只是想知道您是否可以在RegEx中使用两次相同的字符?

Glenn


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

推荐答案

以下正则表达式模式
The following regular expression pattern
string regExp =@"(?<=!).+?(?=!)";


可用于排除前缀和后缀. 在正则表达式模式中,?< =匹配前缀但不包含前缀,在上面的代码中=丢失了.

但是问题是我们在必需单词之间得到了单词,因为排除的后缀将成为下一个单词的前缀,即从下面的示例中获得test1, middle text, othertext,而我认为我们只需要在!!
之间 test1, othertext
以下代码可用于此目的


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

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);
}


在此正则表达式模式中[和]是特殊字符,因此,它们之前必须带有\.
字符串regExp = @(?< = \ [).+?(?= \]);

在这种情况下,由于前缀字符与后缀字符不同,因此不会出现在所需单词之间捕获单词的问题.
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.
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);
    }
}


这篇关于正则表达式的疯狂,还是如何使其可变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆