正则表达式仅在golang中屏蔽任何匹配10位数字的字符串 [英] Regular Expression to mask any string matching 10 digits only in golang

查看:137
本文介绍了正则表达式仅在golang中屏蔽任何匹配10位数字的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于golang正则表达式不支持先行,我想知道有什么方法可以创建一个正则表达式来屏蔽任何具有10位数字的字符串.

Since golang regex does not support lookaheads, I was wondering is there any way i can create a regex that will mask any string having a 10 digit number.

func main() {
    s := "arandomsensitive information: 1234567890 this is not senstive: 1234567890000000"
    re := regexp.MustCompile(`\d{10}`)
    s = re.ReplaceAllString(s, "$1**********$2")
    fmt.Println(s)
}

是否可以获得这样的输出随机敏感信息:1234 ******,这并不敏感:1234567890000000"

Is it possible to get an output like this "arandomsensitive information: 1234****** this is not senstive: 1234567890000000"

还有可以尝试的无正则表达式的正则表达式吗?

Also any regex without lookaheads that i can try?

推荐答案

如果您知道10位数字只能出现在单词边界之间,即字母,数字或下划线以外的字符之间,则可以使用 ReplaceAllString :

If you know the 10-digit number can only appear in between word boundaries - that is, between characters other than letters, digits or underscores - you may use a simple word boundary approach with ReplaceAllString:

\b(\d{4})\d{6}\b

替换为 $ 1 ****** .请参见在线正则表达式演示.

\ b(\ d {4})\ d {6} \ b 模式首先匹配一个单词边界,然后匹配并捕获四位数字成组1,然后匹配任意六位数字,并然后需要一个单词边界位置.

The \b(\d{4})\d{6}\b pattern matches a word boundary first, then matches and captured four digits into Group 1, then matches any six digits and then requires a word boundary position.

请参见进行演示:

package main

import (
    "fmt"
    "regexp"
)

func main() {
    s := "arandomsensitive information: 1234567890 this is not senstive: 1234567890000000"
    re := regexp.MustCompile(`\b(\d{4})\d{6}\b`)
    s = re.ReplaceAllString(s, "$1******")
    fmt.Println(s)
}

如果需要在任何非数字字符之间匹配10位数字,则可以使用

If you need to match the 10-digit number in between any non-digit characters, you may use

package main

import (
    "fmt"
    "regexp"
)

func main() {
    s := "aspacestrippedstring1234567890buttrailingonehouldnotbematchedastitis20characters12345678901234567890"
    re := regexp.MustCompile(`((?:\D|^)\d{4})\d{6}(\D|$)`)
    fmt.Println(re.ReplaceAllString(s, "$1******$2"))
}

请参见进行演示

注意:由于Golang正则表达式不支持环视功能,因此无法在一个步骤中使用正则表达式来处理连续的数字.提前(?!\ d)可以匹配 1234567890 1234567891 字符串中的两个数字.因此,没有像这样的连续匹配来解决问题的纯正则表达式方法.但是,您可以运行两次regex replace 来解决它:

NOTE: Since Golang regex does not support lookarounds, it is impossible to handle consecutive numbers with a regex in a single step. A (?!\d) lookahead would make it possible to match both numbers in 1234567890 1234567891 string. So, there is no pure regex way of solving the problem with consecutive matches like this. However, you may run regex replace twice to solve it:

result := re.ReplaceAllString(re.ReplaceAllString(s, "$1******$2"), "$1******$2")

正则表达式详细信息:

  • ((?:\ D | ^)\ d {4})-组1:任何非数字字符或字符串开头,然后是任意4位数字
  • \ d {6} -任意六位数字
  • (\ D | $)-第2组:任何非数字或字符串结尾.
  • ((?:\D|^)\d{4}) - Group 1: any non-digit char or start of string and then any 4 digits
  • \d{6} - any six digits
  • (\D|$) - Group 2: any non-digit or end of string.

这篇关于正则表达式仅在golang中屏蔽任何匹配10位数字的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆