开发者

Matching word when not followed by number or space+number

I wanna match "on" as long as it's not followed by a number or a space+number - without using negative lookaheads (because as far as I can tel开发者_StackOverflowl, C doesn't support this - please correct me if I'm wrong).

Thanks!


This works:

#include <sys/types.h>
#include <regex.h>
regex_t re;
/* regcomp(&re, "on([^ ]| [^[:digit:]])", REG_EXTENDED); */ // thanks sln :)
regcomp(&re, "on ?[^ 0-9]", REG_EXTENDED);


If you want to write a matcher in C but without regex, then you might want to take a look at isspace(), isdigit() and isalpha() in ctype.h


You want to match a string that has your conditions in it?

edit - didn't read ? very well

edit - Ok, here ya go buddy

/on([^\d ]|[ ](\D|$)|$)/

Test case:

use strict; use warnings;

my @samps = (
' on',
' on9',
' on ',
' on 5',
' on  7',
' on p',
' on6 ',
);

for (@samps) {
   if ( /on([^\d ]|[ ](\D|$)|$)/ ) { 
         print "matched '$_'\n";
   }
   else {
         print "'$_' failed\n";
   }      
}

Output

matched ' on'
' on9' failed
matched ' on '
' on 5' failed
matched ' on  7'
matched ' on p'
' on6 ' failed
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜