English 中文(简体)
3. 需要定期表达,以适应用户生成的模式
原标题:Need regular expression to match user generated patterns

我有一个伙伴关系。 在C#(3.5框架)中,用户向我提供了我们围绕规则制定的模式清单。 我可以撰写法典,对每一种模式进行人工核算,但更愿意找到一种方法,使用常规表述(或实际采用的任何方法)来处理这些表述,以便我能够让用户在今后创造更多的模式(按照某些准则)。

我将尽全力以赴地展示我努力要做的事情。 我确实赞赏你的帮助。

下面是几个模式:

Pattern 1: Element 1: CxxxxRxxxx Element 2: CxxxxRzzzz Result: Bucket 1

Pattern 2: Element 1: CxxxxRxxxx
Element 2: CxxxxRxxxx Result: Bucket 2

Pattern 3: Element 1: PCxxxxxxxx
Element 2: PCzzzzzzzz Result: Bucket 3

Pattern 4: Element 1: PCxxxxxxxx
Element 2: UxxxxRxxxx Result: Bucket 4

在这方面,他们意味着什么,我需要怎样处理。 对于开端人来说,所有要素总长为10个。 每一形态的字母特性保持不变。 X和Z可以是编号或字母。 X和Z在形式上签字的意思是,2个要素的其他部分是否相互匹配。

例如,我的投入数据是作为要素1的“C1234R5678”和要素“C1234R9999”。 2. 在这种情况下,结果将是“Bucket 1”,因为这符合为这一条件所确定的模式(“C”类对应数字后的数字,但“R”之后的数字没有)。

在另一个例子中,我的投入数据是作为要素1的“C1234R5678”和要素“C1234R5678”。 2. 在这种情况下,结果将是“Bucket 2”,因为这符合为这一条件所确定的模式(“C”类对应数字和“R”对应数字)。

In another example, my input data is "PC12345678" as element 1 and "PC87654321" for element 2. In this case, the result would be "Bucket 3" since this matches the pattern defined for this condition (the numbers after the "PC" do not match).

最后,我的投入数据是作为要素1的“PC12345678”和要素“U1234R5678”。 2. 在这种情况下,结果将是“Bucket 4”,因为这符合为这一状况确定的模式(“PC”之后的4个数字与美国之后的4个数字相匹配,而PC要素最后4位数与要素2的R之后的4位数相匹配)。

如你所看到的那样,这种模式可能有所不同,因为有些时候,你在一封信之后确定4个号码,或者在一封信之后确定整个编号。

感谢你们的时间。

最佳回答

你可以做这样的事情:

For each (Element1,Element2) pair:
    match Element1,Element2 on "(x+|z+)" -> matches1,matches2

    for (i=0;i<min(matches1,matches2);i++) {
        replace  x+  by  ([A-Za-z0-9]{matches1[i].Length}) 
        // note: need the brackets here to act as capturing brackets
        // so that backreferences can be used!

        if matches2[i] is  xxxxx :
            replace  x+  by  (?!i)[A-Za-z0--9]{matches2[i].Length} 
        else if matches2[i] is  zzzzz :
            replace  z+  by  i 
    }
    Then replace miscellaneous leftover  xxxx  by  [A-Za-z0-9]{lengthofmatch} .

    Then join the new Element1 and Element2 by a delimiter e.g.  - 

这将建造一个星座,如:

//Pattern 1
C([A-Za-z0-9]{4})R([A-Za-z0-9]{4})-C(?!1)[A-Za-z0-9]{4}R2
//Pattern 2
C([A-Za-z0-9]{4})R([A-Za-z0-9]{4})-C(?!1)[A-Za-z0-9]{4}R(?!2)[A-Za-z0-9]{4}
//Pattern 3
PC([A-Za-z0-9]{8})-PC1
//Pattern 4
PC([A-Za-z0-9]{8})-U(?!1)[A-Za-z0-9]{4}R[A-Za-z0-9]{4}

然后,每份投入数据对应的Element1和Element2,与(例如C1234R5678-C1234R99>分离,与每种格式相匹配,并停留在第一栏。

问题回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!