Using Regexp or Instr to pull out a substring from a string

  • Thread starter Thread starter richardkreidl
  • Start date Start date
R

richardkreidl

I have the following string below in which I need to pull out the name
that follows: ContactsManager:" .
In this particular case it would be:
BARBARA JOSEPH

Job Completion, Failure Instructions, and Rerun NotesAppl ID
Description:dailyopsEdit Application ContactsManager: BARBARA JOSEPH
- Ext: 2612Primary: ISAAC THOMAS - Ext: 2011Secondary: RON MACDONALD -
Ext: 8759Edit Job ContactsPrimary: JEFF PETERS - Ext: 4908Secondary:
JOHN MAYER - Ext: 5104Tertiary: SUE WINTERS - Ext: 6978If this job
abends, notifyNext AM



thanks
 
Richard,

Here is a way of doing it, but I am sure there is a simplier way too:

Dim strFull As String = "Job Completion, Failure Instructions, and
Rerun NotesAppl ID Description: dailyopsEdit Application ContactsManager:
BARBARA JOSEPH - Ext: 2612Primary: ISAAC THOMAS - Ext: 2011" ' Cut down for
convenience
Dim intJob As Integer = strFull.IndexOf("ContactsManager:") ' Index
= 100
Dim intStart As Integer = intJob + 17 ' Start of name
Dim intHyphen As Integer = strFull.IndexOf("-") - 1 ' After Contacts
Manager's name (trimming the space)
Dim intLength As Integer = intHyphen - intStart ' Length of name
Dim strName As String = strFull.Substring(intStart, intLength) '
Find name
MessageBox.Show(strName, "Application Contacts Manager:") ' Display
 
I have the following string below in which I need to pull out the name
that follows: ContactsManager:" .
In this particular case it would be:
BARBARA JOSEPH

Job Completion, Failure Instructions, and Rerun NotesAppl ID
Description:dailyopsEdit Application ContactsManager: BARBARA JOSEPH
- Ext: 2612Primary: ISAAC THOMAS - Ext: 2011Secondary: RON MACDONALD -
Ext: 8759Edit Job ContactsPrimary: JEFF PETERS - Ext: 4908Secondary:
JOHN MAYER - Ext: 5104Tertiary: SUE WINTERS - Ext: 6978If this job
abends, notifyNext AM



thanks

This seems to work for me. It's using Regex.

Private Function ParseStrings(ByVal strings)
Dim re As New Regex("ContactsManager:\ ([^-]*\b)",
RegexOptions.IgnoreCase Or RegexOptions.Compiled)
Dim myMatch As Match = re.Match(strings)

If (myMatch.Success) Then
Return myMatch.Groups(1).Value
Else
Return "It's not here!"
End If
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim theStrings As String
Dim readStrings As String
Dim readFile As New
System.IO.StreamReader(Application.StartupPath & "\strings.txt")

theStrings = readFile.ReadToEnd()
readStrings = ParseStrings(theStrings)
MsgBox(readStrings) ' Show BARBARA JOSEPH

readFile.Close()

End Sub

I've been able to use this to get the names following all of your
colons. For example...

Dim re As New Regex("Tertiary:\ ([^-]*\b)", RegexOptions.IgnoreCase Or
RegexOptions.Compiled)

This will return SUE WINTERS for me on my machine. Hopefully your
mileage will not vary too much.

I am still myself new to VB .NET, so if this doesn't perform as
expected, I am sorry.

Anyway, hope it DOES help.

Ryan

P.S.
If anyone can show me how to handle this method more efficiently, I
would love to see it. It will only help me learn better. :)
 
Back
Top