开发者

Regex to match non-numbers

I have a f开发者_StackOverflow社区ile in the format, where each line has double numbers seperated by spaces, e.g.:

1 -0.5 0.567 0.123 

What is the correct way to find files not in this format? I've tried:

[^0123456789\.\-\s]

But it did not work, any help?


The following works for me:

> grep '[^0123456789\. \-]' *

But note that this method will not notify you of formatting errors. For example:

1 -1.0.4 .012

is not the same format as what you specified but will not be matched.

EDIT: It looks like you can use the [:space:] character class as well.

> grep '[^0123456789\.[:space:]\-]' *

EDIT: And following that same logic, you can use the [:digit:] character class.

> grep '[^[:digit:]\.[:space:]\-]' *


Try

$ grep -LP '^[\s\d.\-]+$' file1 file2 file3 file4 

which really means

$ grep --files-without-match --perl-regexp '^[\s\d.\-]+$' file1 file2 file3 file4


The reason, upon closer inspection that it didn't work with \s is because \s matches any whitespace characters. Which is equivelant to [ \t\r\n] (though some flavors might include other non-printing characters). And because \n (line break) is included in this, you will get matches on nearly every line.

The only files that probably matched 'correctly' likely had an invalid last line that didn't include a line break.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜