我有一个文本文件,如下所示:
Pascal 14241 Mar 28
我想检查它是否与以下格式匹配:
Name size date
当我运行我的代码时,我得到一个错误:
Can't find Unicode property definition "" at testar.pl line 13, <IN> line 2 (#1
)
(F) You may have tried to use p which means a Unicode
property (for example p{Lu} matches all uppercase
letters). If you did mean to use a Unicode property, see
"Properties accessible through p{} and P{}" in perluniprops
for a complete list of available properties. If you didn't
mean to use a Unicode property, escape the p, either by \p
(just the p) or by Qp (the rest of the string, or
until E).
Uncaught exception from user code:
Can't find Unicode property definition "" at testar.pl line 13, <IN> line 2.
at testar.pl line 13
这是我的代码:
#!/bin/usr/perl
use strict;
use warnings;
use diagnostics;
open (IN, "sample1.txt") or die "cant read words from file: $!";
while (<IN>) {
chomp;
if ($_ =~/pw+s+d+sw+s+d+/){
print "$_ n";
}
}
我该怎么解决这个问题?
p
后面必须跟一个单字符Unicode属性(例如pL
–字母)或一个curlies中的属性(例如,p{Lu}
–大写字母)。
p
不是有效的,因为不是有效的Unicode属性。实际上,在正则表达式中根本不需要
p
。
/w+s+d+sw+s+d+/
如果您打算锚定在线路的起点,请使用^
。
/^w+s+d+sw+s+d+/
您的语句只将输入字符串与正则表达式相匹配。它不捕获任何值(您也没有告诉perl捕获什么是有趣的)。
为了捕捉你应该使用的单词:
/^(w+)s+(d+)s(w+s+d+)/
捕获值将作为$1
用于第一次捕获,$2
用于第二次捕获,依此类推。然后您可以只打印您需要的内容:
print $1." ".$2." ".$3."n";