2

i have the following string

sa=t&rct=j&q=&esrc=s&source=productsearch&cd=1&cad=rja&docid=10829621560421678006&ved=0CDEQ9gIwAA&url=http%3A%2F%2Fwww

i would like to parse this part out 10829621560421678006

i am thinking about writing a function that returns the string between 2 specified strings which are in this case &docid= and &ved=

any suggestions or a better solution?, note that i can't use split since the string structure will differ from time to time, my reference to this number are those 2 strings &docid= and &ved=

also is it possible to use regualr expression?

4 Answers 4

3

You could use Strings.Split Method for this:

Dim extracted As String = Split(Split(sourcestring, "&docid=")(1), "&ved=")(0)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer, your answer is the best for my case ;-D
1

You can try something like this:-

 Dim startIndex As Integer = temp.IndexOf("&docid=") + 20
 Dim endIndex As Integer = temp.IndexOf("&ved=", startIndex)
 Dim extraction As String = temp.Substring(startIndex, endIndex - startIndex).Trim

and if you want to use Regex then you can simply try this:-

 docid=(.*?)&ved

Comments

1

Very simple and effective is this regex.

docid=(.*?)&ved

Result:

$matches Array:
(
    [0] => Array
        (
            [0] => docid=10829621560421678006&ved
        )

    [1] => Array
        (
            [0] => 10829621560421678006
        )

)

Comments

1

You could use LINQ like this:

var docid = 
    text
        .Split('&')
        .Select(x => x.Split('='))
        .Where(x => x[0] == "docid")
        .Select(x => x[1])
        .First();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.