我假设你想像这样转换文本:
ExtnTinNo="1234567891234567"
ExtnTinNo="12345678912345678912345"
到这样的事情:
ExtnTinNo="****4567"
ExtnTinNo="****2345"
如果是这样,您应该尝试以下正则表达式:
(?<=ExtnTinNo=['"])\d{12,}(?=[\d]{4}["'])
这将匹配您号码中的所有数字,最后 4 位除外。也就是说,它将匹配以下内容:
ExtnTinNo="1234567891234567"
^^^^^^^^^^^^
ExtnTinNo="12345678912345678912345"
^^^^^^^^^^^^^^^^^^^
通过简单地使用正则表达式替换,您将得到上面给出的结果。
它是这样工作的:
(?<=ExtnTinNo=['"]) - checks that the number is preceded by ExtnTinNo="
(not included in the match)
\d{12,} - matches 12 or more numbers
(?=[\d]{4}["']) - if these 12 numbers are followed by another 4 numbers
and a " or ' (not included in the match)
请注意,这不包括您的空白问题!根据您的正则表达式引擎,您甚至可以在= 周围添加可选的空格。但是,并非所有引擎都支持可变长度的后视!例如,在 .NET 中应该可以使用:
(?<=ExtnTinNo\s*=\s*['"])\d{12,}(?=[\d]{4}["'])
(见demo here,点击“上下文”标签查看替换后的结果)