Solved

Using regular expressions to mask/format strings

Posted on 2004-03-31
7
1,341 Views
Last Modified: 2008-02-26
We're trying to fina a way to use regular expressions under vb.net to perform masking and formatting functions on data.  For example, we have an object with an 'account number' property to it.  This property has various metadata assigned to it--min value, max value, etc.  What we would like to do is have a 'format' regexp as a string in the metadata that could be applied to the account number property to mask all but the last four digits.  Example:

myObject.AccountID.Value='123456789'

...apply the regexp here...

myObject.AccountID.FormattedValue='*****6789'

We know it will be strictly numeric; ie, no dashes or spaces, but we don't know how many digits the number might be.  I've bounced this off a few knowledgable friends, but so far, nada.  Ideally we'd like to be able to have formatting-type regeps or some sort of string that can be applied to data like this to mask it, format it, insert hyphens, whatever, but this first one is proving to be a pain in the neck.

Kelly
0
Comment
Question by:toymaker
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
7 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 10726932
Was there a reason that you are trying to use regular expressions?  Is this credit card numbers?

      Dim number As String = "123456789"

      number = number.Replace(number.Substring(0, 5), "****")

Bob
0
 

Author Comment

by:toymaker
ID: 10726982
Similar to credit card numbers, but not necessarily.  The idea is not to hardcode the replacement as you're doing; we want to have a single method that we can feed the number, and the regexp, have it apply the latter to the former, and spit out the result.  That way, the formatting rules are defined in data, not in code, so they can be easily changed on the data-side and as I say only a single, general-purpose method would be used to perform the formatting.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 125 total points
ID: 10727030
Okay, then try this:

      Dim number As String = "1234567890"
      Dim length As Integer = 5

      Dim r As New System.Text.RegularExpressions.Regex("^(\d){1," & length & "}")

      Dim result As String = r.Replace(number, New String("*", length))

Bob
0
 

Author Comment

by:toymaker
ID: 10727126
Again, that wouldn't solve the problem of doing different sorts of formatting jobs.  Say I have this function:

function FormatIt (myValue as string,myRegExpression as string,myFormat as string) as string
    Dim re As New Regex(myRegExpression)
    Return re.Replace(myValue,myFormat)
end function

OK.  With that function I can call

x=FormatIt("123456789","(\d{5})(\d{4})","$1-$2")

and x will return "12345-6789".

What I'm looking for here is a regexp/formatting string that I can put as parameters to this function so that it can do the masking as we've discussed.  I personally wouldn't mind hardcoding the formatting rules as you're doing, btw, but the boss wants it all contained in data side.
0
 
LVL 15

Assisted Solution

by:Timbo87
Timbo87 earned 125 total points
ID: 10727468
It's not a regular expression, but it will work on values of any length.

Function HideValue(ByVal number As String, ByVal blank As Integer, ByVal hidden As String) As String
    Dim hide As String = ""
    Dim len As Integer = number.Length - blank
    Dim i As Integer
    For i = 0 To len - 1
        hide = hide & hidden
    Next
    HideValue = number.Replace(number.Substring(0, len), hide)
End Function

Here's an example method call:
HideValue("123456789123456789", 4, "*")

The syntax is
HideValue(value to hide, number of trailing characters to hide, character to replace it with)
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question