Solved

Using regular expressions to mask/format strings

Posted on 2004-03-31
7
1,329 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
  • 2
  • 2
7 Comments
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now