paulandyt
asked on
Retrieve a reference from a string
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?
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
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
ASKER
Using expressions worked!
Here is my code:
Imports System.Collections.Generic
Imports System.Globalization
Imports System.Text.RegularExpress ions
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
Here is my code:
Imports System.Collections.Generic
Imports System.Globalization
Imports System.Text.RegularExpress
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
ASKER
Apologies Robert, your suggestion also worked!
No worries, you figured it out yourself quicker!
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.