?
Solved

Using regular expressions to mask/format strings

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

How to Create Failover DNS Record Sets in Route 53

Route 53 has the ability to easily configure DNS record sets specifically for failover scenarios. These failover record sets can be configured to failover to full-blown deployments in other regions or to a static HTML page that informs your customers of the issue.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

771 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