使用grep过滤掉意大利财务代码

时间:2019-03-13 06:55:53

标签: regex grep

我当前正在使用此正则表达式

/^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$/i

但是由于某些原因,grep完全没有结果。

这是我正在执行的:

grep -E -o "/^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$/i" filename.txt

有关如何生成意大利财务代码的信息:https://en.wikipedia.org/wiki/Italian_fiscal_code_card

样本数据:

fjksdhfdskjhfsdkjfhsjkfhsdMLLSNT82P65Z404Ukjfdshkjfsdhkjfdshfjdshmnbmnb
CF= "mrtmtt25d09f205z" (Region).

预期输出:

MLLSNT82P65Z404U    
mrtmtt25d09f205z

1 个答案:

答案 0 :(得分:1)

使用grep

$ grep -io "[A-Z]\{6\}[0-9]\{2\}[A-Z][0-9]\{2\}[A-Z][0-9]\{3\}[A-Z]" file
MLLSNT82P65Z404U
mrtmtt25d09f205z

$ grep -Eio "[A-Z]{6}[0-9]{2}[A-Z][0-9]{2}[A-Z][0-9]{3}[A-Z]" file

$ grep -Pio "[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]" file

man grep

-i, --ignore-case
       Ignore case distinctions in  both  the  PATTERN  and  the  input
       files.
-o, --only-matching
       Print  only  the  matched  (non-empty) parts of a matching line,
       with each such part on a separate output line.
-E, --extended-regexp
       Interpret PATTERN as an extended regular  expression 
-P, --perl-regexp
       Interpret the pattern as a  Perl-compatible  regular  expression
       (PCRE).   This  is  highly  experimental and grep -P may warn of
       unimplemented features.