正则表达式从开头或结尾掩盖给定的数字

时间:2015-06-25 02:32:55

标签: regex

我有一个场景,必须根据配置的正则表达式模式屏蔽我的应用程序返回的两个数字。我有两个数字,需要屏蔽,如下所示。

  • 20128569 - > 2012 ****
  • 40953186 - > **** 3186

我需要两个正则表达式模式来相应地使用String.replaceAll(...)或其他一些可能的方式来实现这种模式。

public static void main(String[] args) {
    String value = "20128569";
    String pattern = "(?<=.{4}).?" ;

    String formattedValue = value.replaceAll(pattern, "*");
    System.out.println(formattedValue);
}

注意:我需要两个正则表达式模式才能屏蔽数字,如上所示。

但是目前我通过以下代码暂时解决了这个问题。但如果我能通过正则表达式解决这个问题,那就太好了。

        String maskedAccountNumber = Pattern.compile(aRegexPattern).matcher(aKey).replaceFirst(MASK_CHARACTER);

        StringBuilder maskBuffer = new StringBuilder();
        for(int i = 0; i <= aKey.length() - maskedAccountNumber.length() ; i++){
              maskBuffer.append(MASK_CHARACTER);
        }
        return      maskedAccountNumber.replace(MASK_CHARACTER, maskBuffer.toString());

以下是我到目前为止使用的两个正则表达式:

  1. ^ \ d {1,3}
  2. \ d {1,3} $

1 个答案:

答案 0 :(得分:0)

这很容易做到,在什么情况下你想要哪个正则表达式仍然很难理解。

无论哪种方式,#DeltaTopNavigation ul li a:first-child span span { color: #F7901E !important; font-size:large; }并将其替换为<div id="DeltaTopNavigation" class="ms-displayInline ms-core-navigation" role="navigation"> <div id="zz12_TopNavigationMenu" class=" noindex ms-core-listMenu-horizontalBox"> <ul id="zz13_RootAspMenu" class="root ms-core-listMenu-root static"> <li class="static selected"> <a class="static selected menu-item ms-core-listMenu-item ms-displayInline ms-core-listMenu-selected ms-navedit-linkNode" tabindex="0" href="/sites/STPrograms/SitePages/Home.aspx" accesskey="1"> <span class="additional-background ms-navedit-flyoutArrow"><span class="menu-item-text">STPrograms</span> <span class="ms-hidden">Currently selected</span> </span> </a> <ul class="static"> <li class="static"><a class="static menu-item ms-core-listMenu-item ms-displayInline ms-navedit-linkNode" tabindex="0" href="/sites/STPrograms/CBP Master/Forms/AllItems.aspx"><span class="additional-background ms-navedit-flyoutArrow"><span class="menu-item-text">Navigation1</span></span></a></li> </ul> </li> <li class="static ms-verticalAlignTop ms-listMenu-editLink ms-navedit-editArea"><span class="ms-navedit-editSpan" id="zz12_TopNavigationMenu_NavMenu_Edit"><a id="zz12_TopNavigationMenu_NavMenu_EditLinks" class="ms-navedit-editLinksText" href="#" onclick="g_QuickLaunchMenu = null; EnsureScriptParams('quicklaunch.js', 'QuickLaunchInitEditMode', 'zz12_TopNavigationMenu', 1, 1, 0, '\u002fSITES\u002fSTPROGRAMS'); cancelDefault(event); return false;"><span class="ms-displayInlineBlock"><span class="ms-navedit-editLinksIconWrapper ms-verticalAlignMiddle"><img class="ms-navedit-editLinksIcon" src="/_layouts/15/images/spcommon.png?rev=23"></span><span class="ms-metadata ms-verticalAlignMiddle">Edit Links</span></span></a><span id="zz12_TopNavigationMenu_NavMenu_Loading" class="ms-navedit-menuLoading ms-hide"><a id="zz12_TopNavigationMenu_NavMenu_GearsLink" href="#" onclick="HideGears(); return false;" title="This animation indicates the operation is in progress. Click to remove this animated image."><img id="zz12_TopNavigationMenu_NavMenu_GearsImage" src="/_layouts/15/images/loadingcirclests16.gif?rev=23"></a></span><div id="zz12_TopNavigationMenu_NavMenu_ErrorMsg" class="ms-navedit-errorMsg"> </div></span> </li> </ul> </div> </div>,如https://regex101.com/r/uI0zJ6/2所示

(\d*)\d{4}并替换为$1**** https://regex101.com/r/uI0zJ6/3