简体   繁体   中英

How can I negate a regular expression?

I want to know a field if it does not match a certain string:

Case 1: When the previous node is not *_pcm.h , then l1 gives the path to file l2 .

Case 2: When there is no l1 or case 1 is not true, then l is what I need

visit(sec) {
  // case 1
  case \sources(_, "_SOURCES", [*_, l1:\sourceList(_, _, _, p:/_pcm.h/i, _, _),
                l2:\sourceList(_, _, _, n:/_pcm.c/i, _, _), *_]): { 
                    name = determinePath(p, n, f); 
  }
  // case 2
  case \sources(_, "_SOURCES", [*_,  l:\sourceList(_, _, _, n:/_pcm.c/i, _, _) , *_]): {
                    path = getPath(f); 
  }
}

Instead of p:/_pcm.h/i I tried p:./_pcm.h/i but this does not work

You can not directly express this negation inside a pattern, but here is a workaround by adding a when condition to the case in the case you are transforming (ie, using => between pattern and action):

data D = d1(str s) | d2(int n);

D transform(D subject){
    return visit(subject){
                case d1(str s) => d1(s + s) when /abc/ !:= s
    }
}

In the case of a more complex case, you can to the test and use a fail to let the case fail.

case d1(str s): { if(/abc/ !:= s) fail; ... }

Hope this solves your problem.

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