Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need regular expression to pull out substring from string

Status
Not open for further replies.

kre1973

IS-IT--Management
May 5, 2006
47
US
I have the following string below in which I need to pull out the following substring:
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
 
Based on the wording of your question you wouldn't need a RegEx, just [tt]String.IndexOf[/tt]

[vampire][bat]
 
Basically, I need to pull out the name following the words:"ContactsManager:" which is "BARBARA JOSEPH"

I would like to use RegEx just to see how's it done...
 
Code:
  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    Dim ptrn As String = "[A-Za-z]{1,}(?=: " + TextBox2.Text + ")"
    Dim rx As New System.Text.RegularExpressions.Regex(ptrn)
    MessageBox.Show(rx.Match(TextBox3.Text).ToString)

  End Sub

Where TextBox2 contains the name you are looking for and TextBox3 contains the text you are searching.


Hope this helps.

[vampire][bat]
 
Actually, the above code pulls the title based on a provided name as opposed to the other way round - but you should be able to work out how to do it the other way round from the example I've given.

[vampire][bat]
 
I don't what name should go in textbox2, it could be different each time I download a string from a Web site.

But, it should always follow the words:"ContactsManager:
 
As I said before, just turn it around:

Code:
    Dim ptrn As String = "(?<=" + TextBox2.Text + ": )[A-Za-z]{1,} [A-Za-z]{1,}"
    Dim rx As New System.Text.RegularExpressions.Regex(ptrn)
    MessageBox.Show(rx.Match(TextBox3.Text).ToString)


TextBox2 contains the title - that way you can lookup any of the titles.


[vampire][bat]
 
If you only ever want ContactsManager then

[tt]
Dim ptrn As String = "(?<=ContactsManager: )[A-Za-z]{1,} [A-Za-z]{1,}"
[/tt]

and forget about TextBox2.

[vampire][bat]
 
In the following code, the pattern [^-]* matches anything that is not a dash after the string ContactsManager: and zero or more white space characters. Groups(0) is always the entire match and Groups(1) is what is referenced above. I trim it to get rid of the trailing space. Good Luck!
Code:
Imports System.Text.RegularExpressions

Dim text As String = "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 "

MsgBox("~" & GetContactManager(text) & "~")

Private Function GetContactManager(ByVal text As String) As String
    Dim options As RegexOptions = RegexOptions.IgnoreCase
    If Regex.IsMatch(text, "ContactsManager:\s*([^-]*)", options) Then
        Dim match As Match = Regex.Match(text, "ContactsManager:\s*([^-]*)", options)
        Return match.Groups(1).ToString.Trim
    Else
        Return Nothing
    End If
End Function 'GetContactManager
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top