简体   繁体   中英

Regular expression for custom types

I'm interested to create a new grammar and build REGEX for the grammar. Grammar is set of laws that allowing word or not.

For exemple i have create 5 types : A, B, C, D, E.

My grammar is all word that like ABC , ABE , ABDC, ABDE . (A and B must, D optional and need choose C or E).

How can i create REGEX to check if the word is correct for the custom grammar?

(preference for c/c++ or objective C code)

Thanks a lot.

A = ice cream, B = cone, D = whipped cream, C = chocolate and E = vanilla.

So ice cream and cone are a must, whipper cream optional and need to know chocolate or vanilla to accept the expression

Try this

(AB(C|E)D?)|(ABD?(C|E))

For objective C:

NSString *searchedString = @"ice creamconewhipped creamchocolate";//NOTE: here i dont put space
NSString *A = @"ice cream";
NSString *B = @"cone";
NSString *D = @"whipped cream";
NSString *C = @"chocolate";
NSString *E = @"vanila";
NSString *stringRegExp = [NSString stringWithFormat:@"(%@%@(%@|%@)%@?)|(%@%@%@?(%@|%@))",A,B,C,E,D,A,B,D,C,E];//(AB(C|E)D?)|(ABD?(C|E))
NSPredicate *myTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", stringRegExp];
if ([myTest evaluateWithObject: searchedString])
{
    NSLog(@"MATCHING");
}
else
{
    NSLog(@"NOT_MATCHING");
}

Equivalent Case for Class:

HomeViewController *objA = [[HomeViewController alloc]init];
 NSString *className1 = NSStringFromClass([objA class]);

TableViewController *objB = [[TableViewController alloc]init];
NSString *className2 = NSStringFromClass([objB class]);

CustomTextView *objD  = [[CustomTextView alloc]init];
NSString *className4 = NSStringFromClass([objD class]);

CustomTableViewCell *objC = [[CustomTableViewCell alloc]init];
NSString *className3 = NSStringFromClass([objC class]);


NSString *objE;

NSString *searchedStringABDC = [NSString stringWithFormat:@"%@%@%@%@",className1,className2,className3,className4];//CASE ABDC/ ABCD

NSString *A = NSStringFromClass([HomeViewController class]);
NSString *B = NSStringFromClass([TableViewController class]);
NSString *D = NSStringFromClass([CustomTextView class]);
NSString *C = NSStringFromClass([CustomTableViewCell class]);
NSString *E = NSStringFromClass([AssementAndPlanView class]);

NSString *stringRegExp = [NSString stringWithFormat:@"(%@%@(%@|%@)%@?)|(%@%@%@?(%@|%@))",A,B,C,E,D,A,B,D,C,E];//(AB(C|E)D?)|(ABD?(C|E))
NSPredicate *myTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", stringRegExp];
if ([myTest evaluateWithObject: searchedStringABDC])
{
    NSLog(@"MATCHING");
}
else
{
    NSLog(@"NOT_MATCHING");
}

or if you doesn't need the correct order, without using regex , u can check with isKindOfClass . For Example:

if ([objA isKindOfClass:[HomeViewController class]]&&[objB isKindOfClass:[TableViewController class]]&&[objC isKindOfClass:[CustomTableViewCell class]]&&!objD&&!objE)
{
    //case ABC
    NSLog(@"case ABC");
}
else if ([objA isKindOfClass:[HomeViewController class]]&&[objB isKindOfClass:[TableViewController class]]&&[objE isKindOfClass:[AssementAndPlanView class]]&&!objC&&!objD)
{
    //case ABE
    NSLog(@"case ABE");        
}

你可以试试这个常规:

ABD?[CE]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM