Backreference Constructs

 

一、Numbered Backreferences

syntax:\1~\9

using System;
using System.Text.RegularExpressions;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "abcabc-defdef123";
            string pattern = @"(abc)\1-(def)\2";
            string replacement = "";
            string result = Regex.Replace(str, pattern, replacement);
            Console.WriteLine(result);
        }
    }
}

其結果為

說明:

pattern 中的「(abc)\1」的「\1」意思是,再引用第一個群組的內容,

相等於「(abc)abc」,所以整個 pattern 可視為「(abc)abc-(def)def」。

 

二、Named Backreferences

syntax:\k<name> or \k'name' or \<name>

using System;
using System.Text.RegularExpressions;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "abcabc-defdef123";
            string pattern = @"(?<toMatch>abc)\k<toMatch>-(def)\1";
            string replacement = "";
            string result = Regex.Replace(str, pattern, replacement);
            Console.WriteLine(result);
        }
    }
}

其結果為

說明:

使用 Numbered Backreferences 方式很容易搞混現在用的 Backreferences 是第幾個,

可以改以具名群組應用在 Backreferences 上面,這樣就不會搞混了。

 

using System;
using System.Text.RegularExpressions;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "abcabc-defdef-ghtght123";
            string pattern = @"(abc)\1-(?<toMatch>def)\k'toMatch'-(ght)\2";
            string replacement = "";
            string result = Regex.Replace(str, pattern, replacement);
            Console.WriteLine(result);
        }
    }
}

其結果為

說明:

當有使用具名群組時,Numbered Backreferences 所取的順序並不會把他算進去。

 

三、Named numeric backreferences

using System;
using System.Text.RegularExpressions;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "abcabc-defdef123";
            string pattern = @"(?<8>abc)\<8>-(def)\1";
            string replacement = "";
            string result = Regex.Replace(str, pattern, replacement);
            Console.WriteLine(result);
        }
    }
}

其結果為

說明:

Named Backreferences 的名稱可以是由數字組成的字串。

 

四、

在沒有名稱衝突下,並且 Backreferences 的前面是具名群組,

而且該 Backreferences 是排在第一分組的條件下,

以下三個範例皆相等

using System;
using System.Text.RegularExpressions;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "abcabc-defdef123";
            string pattern = @"(?<toMatch>abc)\1-(?<tom>def)\k<tom>";
            string replacement = "";
            string result = Regex.Replace(str, pattern, replacement);
            Console.WriteLine(result);
        }
    }
}

其結果為

 

using System;
using System.Text.RegularExpressions;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "abcabc-defdef123";
            string pattern = @"(?<toMatch>abc)\k<1>-(?<tom>def)\k<tom>";
            string replacement = "";
            string result = Regex.Replace(str, pattern, replacement);
            Console.WriteLine(result);
        }
    }
}

其結果為

 

using System;
using System.Text.RegularExpressions;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "abcabc-defdef123";
            string pattern = @"(?<toMatch>abc)\<1>-(?<tom>def)\k<tom>";
            string replacement = "";
            string result = Regex.Replace(str, pattern, replacement);
            Console.WriteLine(result);
        }
    }
}

其結果為

 

五、

如果具名群組是由數字組成的字串名稱的話,則第四項的觀點不能被套用。

using System;
using System.Text.RegularExpressions;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "abcabc-defdef123";
            string pattern = @"(?<2>abc)\k<1>-(?<tom>def)\k<tom>";
            bool result = Regex.IsMatch(str, pattern);
            Console.WriteLine(result);
        }
    }
}

其結果為

 

六、其他範例

using System;
using System.Text.RegularExpressions;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string pattern = @"(?<1>a)(?<1>\1b)*";
            string input = "aababb";
            foreach (Match match in Regex.Matches(input, pattern))
            {
                Console.WriteLine("Match: " + match.Value);
                foreach (Group group in match.Groups)
                    Console.WriteLine("   Group: " + group.Value);
            }
        }
    }
}

其結果為

 

參考資料:

Backreference Constructs in Regular Expressions