1

我需要@[Alphanumeric1](Alphanumeric2:Alphanumeric3)从长字符串中解析以下格式。下面是我的字符串:

This is a long text 
@[Alphanumeric1](Alphanumeric2:Alphanumeric3) again long text 
@[Alphanumeric11](Alphanumeric22:Alphanumeric33) again long text
@[Alphanumeric111](Alphanumeric222:Alphanumeric333) 

我需要将所有出现的( @[Alphanumeric1](Alphanumeric2:Alphanumeric3)) 替换为冒号(:) 之后的值,即我希望输出为

This is a long text 
Alphanumeric3 again long text 
Alphanumeric33 again long text
Alphanumeric333 
4

3 回答 3

2

@\[[\w\d]*\]\([\w\d]*:([\w\d]*)\)

这将匹配上面的三个字符串,并:在组1之后获取字母数字字符串。在这里玩正则表达式

于 2012-07-27T14:41:37.793 回答
0

以下正则表达式应该能够处理该输入:

(@\[[a-zA-Z0-9]+\]\([a-zA-Z0-9]+:(?<match>[a-zA-Z0-9]+)\))

在 C# 中使用,以下将查找并替换字符串中的所有实例input

string output = Regex.Replace(input, @"(@\[[a-zA-Z0-9]+\]\([a-zA-Z0-9]+:(?<match>[a-zA-Z0-9]+)\))", "${match}");

正则表达式解释:

(                           # beginning of group to find+replace
    @                       # match the '@'
    \[                      # match the '['
        [a-zA-Z0-9]+        # alpha-numeric match
    \]                      # match the ']'
    \(                      # match the '('
        [a-zA-Z0-9]+        # alpha-numeric match
        :                   # match the ':'
        (?<match>           # beginning of group to use for replacement
            [a-zA-Z0-9]+    # alpha-numeric match
        )
    \)                      # match the ')'
)
于 2012-07-27T14:44:46.260 回答
0

这应该可以解决问题:

class Program
{
    static void Main(string[] args)
    {
        string data = @"This is a long text 
@[Alphanumeric1](Alphanumeric2:Alphanumeric3) again long text 
@[Alphanumeric11](Alphanumeric22:Alphanumeric33) again long text
@[Alphanumeric111](Alphanumeric222:Alphanumeric333)";

        Debug.WriteLine(ReplaceData(data));
    }

    private static string ReplaceData(string data)
    {
        return Regex.Replace(data, @"@\[.+?\]\(.*?:(.*?)\)", match => match.Groups[1].ToString());
    }
}
于 2012-07-27T14:45:57.797 回答