具有多个可能选项的正则表达式

regex with multiple possible options

我正在努力完成这个:

此脚本应读取当前目录并打印任何以消息开头且可能后跟句点和数字的文件的内容。

opendir(CURRENT, ".");
@message_file = grep(/message(\.\d+)?/, readdir (CURRENT));
closedir(CURRENT);

foreach $message_file (@message_file) {
        open (FILE, $message_file);
        while(<FILE>){
             print;
}
close(FILE);
}

我的代码仍然打印出 message.txt 这样的文件。我试图找出正确的语法来不包含诸如此类的文件,并且只包含带有句点和数字的文件。

有什么想法吗?

谢谢。

I am trying to figure out the proper syntax to not include files such as that and only ones with a period and a digit after.

您可以使用这个基于 负前瞻 的正则表达式:

/^message(?!\.\D)/
  • (?!\.\D) 表示如果 messages 后跟一个点和非数字则匹配失败。
  • 也最好使用锚 ^ 作为开始