Regex 使用grep过滤意大利财政代码
我目前正在使用这个正则表达式Regex 使用grep过滤意大利财政代码,regex,grep,Regex,Grep,我目前正在使用这个正则表达式 /^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$/i 但出于某种原因,格雷普根本没有给我任何结果 这就是我正在执行的: grep -E -o "/^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$/i" filename.txt 有关如何生成意大利财政代码的信息: 样本数据: fjksdhfdskjhfsdkjfhsjkfhsdMLLSNT82P65Z404Ukjfdshkjfsdhkjfdshfj
/^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$/i
但出于某种原因,格雷普根本没有给我任何结果
这就是我正在执行的:
grep -E -o "/^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$/i" filename.txt
有关如何生成意大利财政代码的信息:
样本数据:
fjksdhfdskjhfsdkjfhsjkfhsdMLLSNT82P65Z404Ukjfdshkjfsdhkjfdshfjdshmnbmnb
CF= "mrtmtt25d09f205z" (Region).
预期产出:
MLLSNT82P65Z404U
mrtmtt25d09f205z
使用
grep
:
$ grep -io "[A-Z]\{6\}[0-9]\{2\}[A-Z][0-9]\{2\}[A-Z][0-9]\{3\}[A-Z]" file
MLLSNT82P65Z404U
mrtmtt25d09f205z
-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.
或
或
man grep
:
$ grep -io "[A-Z]\{6\}[0-9]\{2\}[A-Z][0-9]\{2\}[A-Z][0-9]\{3\}[A-Z]" file
MLLSNT82P65Z404U
mrtmtt25d09f205z
-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.
一些具有预期输出的示例数据如何。顺便说一句,不是我的反对票。我已经更新了我的问题@JamesBrown。谢谢@Cyrus,已经更新了问题!您需要同时删除
^
和$
,并在i
之外使用g
标志。在使用正则表达式的位置显示实际代码。请注意,grep
不接受/…/
符号,/
字符被视为普通字符,要使其与PCRE-P
选项一起工作,需要将其修改为(?i)^[a-Z]{6}\d{2}[a-Z]\d{2}[a-Z]\d{3}[a-Z]$/code>