Click here to Skip to main content
15,904,156 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello I need a bit of help to understand if its possible to match a string with regular expression but only to show the middle or next word to the string?

for example the format looks like

"Today Date: Monday23," or "Today Date Monday 23,"


I can successfully find and display the words "Today Date" but the day following and number are unknown.

What would be the regular expression for showing the day and number which is between "Today Date" and the end of the string which is a ","


thank you in advance!
Posted

1 solution

You can use the following regular expression:
VB
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim text As String = "Today Date: Monday23,"

    text = GetMatchingText(text) 'You can replace text with some other String like for instance TextBox1.Text
End Sub

Private Function GetMatchingText(stringValue As String) As String
    Dim pattern As String = "Today Date\:? ([A-Za-z]+ ?\d+),"
    Dim result As String = String.Empty

    If Regex.IsMatch(stringValue, pattern) Then
        'Example assumes you have only one result.
        result = Regex.Match(stringValue, pattern).Groups(1).Value
    End If

    Return result
End Function


Replace your orginal string value with the return value of the function GetMatchingText.

A small explanation:
* the semicolon is optional so you can detect it with \:? (the question mark indicates it is optional)
* with the parenthesis ([A-Za-z]+ ?\d+) defines a group and the results you want
* [A-Za-z]+ indicates characters A to Z in both upper and lower case. The + defines 1 or more matches
* the \d+ is one or more digital numbers

The results can be accessed using the Groups collection. Groups(0) results in the entire match. The result you want is in the first group.
 
Share this answer
 
v2
Comments
Dale 2012 9-Sep-12 22:21pm    
thank you for this explanation I have one other question to this. The date Monday 23 is not always constant and changes from time to time to what ever day and number of day relates to the string which is unknown. Will this display anything no matter if it changes?
Martijn Kok 10-Sep-12 1:12am    
It will also detect every combination of 1 word and 1 numberic value
Dale 2012 9-Sep-12 22:31pm    
also the regular expression returns the word Form1 for some reason and not the string I am searching for any ideas?
Martijn Kok 10-Sep-12 1:21am    
I haven't enough information to know what is the bug. If the code is placed in a form called Form1, then it looks like the name of your form gets in the result. You'll need to debug your code. Place some breakpoints and try to find where the result changes into Form1.
Dale 2012 9-Sep-12 22:51pm    
the format might also be like so...

Today Date: Monday-23-2012 or
Today Date: Monday.23-2012

thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900