By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,045 Members | 1,308 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,045 IT Pros & Developers. It's quick & easy.

Find what comes after a match in a string

P: 2
Hi.

I'm trying to find out (isolate) the numbers that following a certain text match in a string.

Example: "pageid=4&ctl=allowed&sectionid=235&numberid=5 5"

In this example, I want to find the text match of "sectionid=". I then want whatever numbers are are after it - in this case it would be "235". But I need ONLY the numbers that fall between "sectionid=" and "&numberid=", since the numbers could be any amount (eg. 235, 2000000, 2, etc).

Thank you for your time and assistance!!!
Apr 10 '15 #1

✓ answered by IronRazer

You can do this using a few of the String methods or by using RegularExpressions. It would be best to learn the String methods before jumping into RegularExpressions though. That can be a little confusing if you are not used to it.

Here is a simple example you can check out.
Expand|Select|Wrap|Line Numbers
  1.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.         Dim str As String = "pageid=4&ctl=allowed&sectionid=235&numberid=5 5"
  3.  
  4.         Dim find As String = "sectionid="
  5.         Dim startindex As Integer = str.IndexOf(find)
  6.         Dim endindex As Integer = str.IndexOf("&", startindex)
  7.  
  8.         Dim number As String = str.Substring(startindex + find.Length, endindex - (startindex + find.Length))
  9.  
  10.         MessageBox.Show(number)
  11.     End Sub
  12.  

Share this Question
Share on Google+
2 Replies


IronRazer
P: 82
You can do this using a few of the String methods or by using RegularExpressions. It would be best to learn the String methods before jumping into RegularExpressions though. That can be a little confusing if you are not used to it.

Here is a simple example you can check out.
Expand|Select|Wrap|Line Numbers
  1.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.         Dim str As String = "pageid=4&ctl=allowed&sectionid=235&numberid=5 5"
  3.  
  4.         Dim find As String = "sectionid="
  5.         Dim startindex As Integer = str.IndexOf(find)
  6.         Dim endindex As Integer = str.IndexOf("&", startindex)
  7.  
  8.         Dim number As String = str.Substring(startindex + find.Length, endindex - (startindex + find.Length))
  9.  
  10.         MessageBox.Show(number)
  11.     End Sub
  12.  
Apr 12 '15 #2

P: 2
Ahhh ... perfect! Thank you!

I was trying to do this with a method of ".Split("sectionid=") to parse out the info. Mistake.

Your solution is exactly where I should have been going from the start.
Apr 13 '15 #3

Post your reply

Sign in to post your reply or Sign up for a free account.