Solved

Retrieve a reference from a string

Posted on 2013-01-29
7
188 Views
Last Modified: 2013-01-29
I have a string and I want to look for a reference in a specific format which could be anywhere in the string. This reference is random, but will always be in the same format.

The format looks like this. N######NN##          

The string would look like this.

Dim MyString as String = "this is my string Z123456TW00 and I need to obtain the reference"

My aim is to take this value and compare it to a reference in my database.

Any ideas how I can obtain anything in the format above regardless of the numbers or letters?
0
Comment
Question by:paulandyt
7 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 38830323
If there is no logic in your strings to get the reference, like for example it always start with a "Z" and ends when a space (or end of the string) is found, then it will be impossible to do.

Perhaps you can perform it in the other direction.

Take the reference from your database and check if that is a part of your string.
0
 
LVL 16

Accepted Solution

by:
Easwaran Paramasivam earned 500 total points
ID: 38830402
Hey you could use Regex to find out the pattern you want.


^\w\d{6}\w{2}\d{2}$  could be your pattern. Please do refer:

http://msdn.microsoft.com/en-us/library/hs600312.aspx
http://www.codeproject.com/Articles/9099/The-30-Minute-Regex-Tutorial
0
 
LVL 1

Expert Comment

by:DoutorApedeuta
ID: 38830419
An easy way, I think, will be to split the string by the whitespace character, and then with the resulting strings do:
1. compare each agains the database, or;
2. validate the format of each string, to search the one that corresponds to the reference, and then compare it to the database.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 35

Expert Comment

by:Robert Schutt
ID: 38830422
EaswaranP is close but I think you need this:
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim MyRef As String = GetRef("this is my string Z123456TW00 and I need to obtain the reference")
        MsgBox(MyRef)
    End Sub

    Private Function GetRef(ByVal MyString As String)
        Dim Matches = Regex.Matches(MyString, "\b\w\d{6}\w{2}\d{2}\b")
        Dim ret As String
        If Matches.Count > 0 Then
            ret = Matches(0).Value
        Else
            ret = "N/A"
        End If
        Return ret
    End Function

Open in new window

0
 

Author Closing Comment

by:paulandyt
ID: 38830424
Using expressions worked!

 Here is my code:

Imports System.Collections.Generic
Imports System.Globalization
Imports System.Text.RegularExpressions

Public Module Example

    Public Sub Main()
        Dim pattern As String = "\w{1}\d{6}\w{4}\b"
        Dim input As String = "this is my string Z123456TW00 and I need to obtain the reference"
        For Each match As Match In Regex.Matches(input, pattern, RegexOptions.IgnoreCase)
            Console.WriteLine("{0}", _
                              match.Value, match.Groups(1).Value, match.Index)
        Next
        Console.Read()
    End Sub

End Module
0
 

Author Comment

by:paulandyt
ID: 38830441
Apologies Robert, your suggestion also worked!
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 38830448
No worries, you figured it out yourself quicker!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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:…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

746 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

13 Experts available now in Live!

Get 1:1 Help Now