[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Using regular expressions to mask/format strings

Posted on 2004-03-31
7
Medium Priority
?
1,372 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
5 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 500 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 500 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

872 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