Solved

Using regular expressions to mask/format strings

Posted on 2004-03-31
7
1,338 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
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

821 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