如何建立长正则表达式

通常,当您使用正则表达式提取文本时,您将从简单表达式开始。 当您了解目标文本时,就可以扩展表达式。 随后很难准备好长的特殊符号集,并且无法改善这种表达方式。

我们必须创建“智能”正则表达式。 我们不用编写一个行表达式,而是准备多行文本,然后从中生成长表达式。 这是一个简单的例子。


space                    [\s/-]+
word                     \w+
words                    (?:{word}{space})*?{word}
birthday                 (?<birthday>\d+\.d+\.d+)
title                    {word}\.
name                     {words}
person                   {title}{space}{name}{space}{birthday} 
此文本包含两列,各列之间用空格分隔。 第一列是模式名称,第二列易于阅读正则表达式。 模式“人”的结果正则表达式为:

\w+\.[\s/-]+(?:\w+[\s/-]+)*?\w+[\s/-]+(?<birthday>\d+.\d+.\d+) 
您可以使用以下课程进行操作

public class Lexer
    {
        private NameValueCollection col;
        public Lexer()
        {
            col = new NameValueCollection();
        } 
        public static Lexer Create(string resource)
        {
            StringReader sr = new StringReader(resource);
            Lexer lex =new Lexer();
            while (sr.Peek()>=0)
            {
                string line = sr.ReadLine();
                Match m = Regex.Match(line,@"([\w_]+)\s+(.*)");
                if (m.Success) 
                {
                    lex.col.Add(m.Groups[1].Value.Trim(), m.Groups[2].Value.Trim());
                }
            }
            sr.Close(); 
            return lex;
        }  
        public string GetExpression(string name)
        {
            if (name == null || name.Length == 0) return string.Empty;
            string res = col[name];
            if (res == null) throw new ArgumentException("Template not found", name); 
            bool needGroup = res.IndexOf('|') > 0;
            Regex reg = new Regex(@"(?<!\\p){([a-zA-Z][\w_]+)}");
            Match m = reg.Match(res);
            while (m.Success)
            {
                string token = m.Groups[1].Value;
                string exp = GetExpression(token); 
                if (exp != null && exp.Length>0)
                    res = res.Replace(@"{"+token+"}",exp);
                m = m.NextMatch();
            }
            string result = res;
            if (needGroup)
            {
                result = "(?:" + res + ")";
            }
            result = "(?#" + name + ")" + result; 
            return result;
        } 
    } 

然后我们可以创建类实例并获取正则表达式


Lexer lex = Lexer.Create(txtLexerText.Text);
string expr = lex.GetExpression("person");
Regex reg = new Regex(expr); 

From: https://bytes.com/topic/net/insights/729580-how-build-long-regular-expression

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值