Results 1 to 11 of 11

Thread: Problem with extract string from an expression

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2020
    Posts
    8

    Exclamation Problem with extract string from an expression

    Hi all,

    I am using vb.net language in an automation software.

    I would like to extract a string from an expression but it display "startIndex cannot be larger than length of string." error message for the few codes that I have used.

    The expression is as follow:

    Name: Lucas Lion
    Age: 48
    TaxID: R935279
    Nationality: Germany

    - First I need to assign the above expression as A, because it is a block of text together.
    - The item that I would like to get is the value of the TaxID, that is R935279.
    - I assign the value of the TaxID as Result.

    Those codes that I have used is as follow:

    Result = A.IndexOf("TaxID: ").TrimStart("TaxID: ").Split(Environment.NewLine)(2)

    Result = A.Split(cchar(Environment.NewLine))(2).Split(cchar(":"))(1)

    Result = A.Substring(ClientInformation.IndexOf("TaxID: ")+"TaxID: ".Length).Split(Environment.NewLine.ToCharArray)(2)

    Result = A.Substring(ClientInformation.IndexOf("TaxID:")+"TaxID:".Length).Split(Environment.NewLine.ToCharArr ay)(2)

    Could you suggest me where I can improve on the run to get the value of the TaxID?

    Thank you in advance.

  2. #2
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,426

    Re: Problem with extract string from an expression

    Hi Lucasi,
    Sadly we need some further information before we can help.

    Where in your code is:
    Name: Lucas Lion
    Age: 48
    TaxID: R935279
    Nationality: Germany
    Is this input from elsewhere ?
    Is it four separate inputs ?
    Is it all in one line ?
    Name: Lucas Lion Age: 48 TaxID: R935279 Nationality: Germany
    Is it separated with, for example, commas ?
    Name: Lucas Lion, Age: 48, TaxID: R935279, Nationality: Germany
    If you can show us what you've done already, that would help.

    To show us your code:
    1. From a new line, paste your code in the Reply page.
    2. Select the whole of your code (Highlight it in the usual way).
    3. Click the # icon in the tool strip above your page.

    This will put 'CODE' and '/CODE' in square brackets around your code.


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Problem with extract string from an expression

    Debug your code. Set one or more breakpoints and step through the code as required. When the exception is thrown, what is the value for startIndex and what is the length of the String? Once you know that, you can work backwards to see where the incorrect index came from. You generally don't fix issues simply by reading the code, because the behaviour of the code depends on the data in use at run time. That's why your integrated development environment (IDE) has a debugger integrated into it. You need to use it.

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2020
    Posts
    8

    Re: Problem with extract string from an expression

    Quote Originally Posted by Poppa Mintin View Post
    Hi Lucasi,
    Sadly we need some further information before we can help.

    Where in your code is:

    Is this input from elsewhere ?
    Is it four separate inputs ?
    Is it all in one line ?

    Is it separated with, for example, commas ?


    If you can show us what you've done already, that would help.

    To show us your code:
    1. From a new line, paste your code in the Reply page.
    2. Select the whole of your code (Highlight it in the usual way).
    3. Click the # icon in the tool strip above your page.

    This will put 'CODE' and '/CODE' in square brackets around your code.


    Poppa
    Quote Originally Posted by jmcilhinney View Post
    Debug your code. Set one or more breakpoints and step through the code as required. When the exception is thrown, what is the value for startIndex and what is the length of the String? Once you know that, you can work backwards to see where the incorrect index came from. You generally don't fix issues simply by reading the code, because the behaviour of the code depends on the data in use at run time. That's why your integrated development environment (IDE) has a debugger integrated into it. You need to use it.

    Hi to both of you.

    I am creating an example in my initial thread.

    Here is how the software looks like. It is a workflow so we need to use vb.net language to process it and the aim in this workflow is to extract from the expression to get the desire string. In my actual workflow I want to extract the TaxID value from the TaxID from, that you can see in the first square box, that is similar to my example.
    Attachment 175653Attachment 175653

    Like I said in my first thread. I have assign the information in the first square box as a variable, in my case is ClientInformation. It does not have comma and not join together. The expression is listed by row.

    I am not use I do not have permission to post the second picture in this post and I will continue in the follow post.

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2020
    Posts
    8

    Re: Problem with extract string from an expression

    Quote Originally Posted by Poppa Mintin View Post
    Hi Lucasi,
    Sadly we need some further information before we can help.

    Where in your code is:

    Is this input from elsewhere ?
    Is it four separate inputs ?
    Is it all in one line ?

    Is it separated with, for example, commas ?


    If you can show us what you've done already, that would help.

    To show us your code:
    1. From a new line, paste your code in the Reply page.
    2. Select the whole of your code (Highlight it in the usual way).
    3. Click the # icon in the tool strip above your page.

    This will put 'CODE' and '/CODE' in square brackets around your code.


    Poppa
    Quote Originally Posted by jmcilhinney View Post
    Debug your code. Set one or more breakpoints and step through the code as required. When the exception is thrown, what is the value for startIndex and what is the length of the String? Once you know that, you can work backwards to see where the incorrect index came from. You generally don't fix issues simply by reading the code, because the behaviour of the code depends on the data in use at run time. That's why your integrated development environment (IDE) has a debugger integrated into it. You need to use it.

    Hi to both of you.

    I am creating an example in my initial thread.

    Here is how the software looks like. It is a workflow so we need to use vb.net language to process it and the aim in this workflow is to extract from the expression to get the desire string. In my actual workflow I want to extract the TaxID value from the TaxID from, that you can see in the first square box, that is similar to my example.
    Attachment 175653Attachment 175653

    Like I said in my first thread. I have assign the information in the first square box as a variable, in my case is ClientInformation. It does not have comma and not join together. The expression is listed by row.

    Then in the second box, I have assigned the extracted TaxID value as TaxID. My 2 vb.net codes below show similar result when they extract the TaxID value:
    Code:
    ClientInformation.Substring(ClientInformation.IndexOf("Client ID:")+"Client ID:".Length).Split(Environment.NewLine.ToCharArray)(0)
    Code:
    ClientInformation.Substring(ClientInformation.IndexOf("Client ID: ")+"Client ID: ".Length).Split(Environment.NewLine.ToCharArray)(0)
    Attached Images Attached Images  
    Last edited by lucasi; Mar 30th, 2020 at 10:41 PM. Reason: left out some information

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2020
    Posts
    8

    Re: Problem with extract string from an expression

    Sorry,

    Please ignore my last 2 posts.

    Here is the correct one.

    I am creating an example in my initial thread.

    Here is how the software looks like. It is a workflow so we need to use vb.net language to process it and the aim in this workflow is to extract from the expression to get the desire string. In my actual workflow I want to extract the TaxID value from the TaxID from, that you can see in the first square box, that is similar to my example.
    Name:  p1.jpg
Views: 376
Size:  11.5 KB

    The original information from the workflow is from the website called ACME. The original information is like this:
    Name:  p2.jpg
Views: 285
Size:  12.7 KB
    You can find this sign by register an account with this link https://acme-test.uipath.com/account/login

    Like I said in my first thread. I have assign the information in the first square box as a variable, in my case is ClientInformation. It does not have comma and not join together. The expression is listed by row.

    Then in the second box, I have assigned the extracted TaxID value as TaxID. The vb.net codes below have been used to extract the TaxID value below.

    The third box is to print out the value of the extracted TaxID.

    Code:
    ClientInformation.Split(CChar(" "))(1).Split(CChar(Environment.NewLine))(0)
    Code:
    ClientInformation.Split(cchar(Environment.NewLine))(0).Split(cchar(":"))(1)
    Code:
    ClientInformation.Substring(ClientInformation.IndexOf("TaxID:")+"TaxID:".Length).Split(Environment.NewLine.ToCharArray)(0)
    Code:
    ClientInformation.Substring(ClientInformation.IndexOf("TaxID:")+"TaxID:".Length).Split(Environment.NewLine.ToCharArr ay)(0)
    Code:
    ClientInformation.IndexOf("TaxID: ").TrimStart("TaxID: ").Split(Environment.NewLine)(0)
    However, all of them showing the same result, that is
    Name:  error.jpg
Views: 378
Size:  23.0 KB

    The startIndex show refer to the second box which is TaxID, the length of the TaxID is fixed with 8 digits (the starting 2 digit is an alphabet and the rest is number)

    I hope master can advise on the error of the vb.net syntax

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2020
    Posts
    8

    Re: Problem with extract string from an expression

    Sorry,

    Please ignore my last 2 posts.

    Here is the correct one.

    I am creating an example in my initial thread.

    Here is how the software looks like. It is a workflow so we need to use vb.net language to process it and the aim in this workflow is to extract from the expression to get the desire string. In my actual workflow I want to extract the TaxID value from the TaxID from, that you can see in the first square box, that is similar to my example.
    Name:  p1.jpg
Views: 376
Size:  11.5 KB

    The original information from the workflow is from the website called ACME. The original information is like this:
    Name:  p2.jpg
Views: 285
Size:  12.7 KB
    You can find this sign by register an account with this link https://acme-test.uipath.com/account/login

    Like I said in my first thread. I have assign the information in the first square box as a variable, in my case is ClientInformation. It does not have comma and not join together. The expression is listed by row.

    Then in the second box, I have assigned the extracted TaxID value as TaxID. The vb.net codes below have been used to extract the TaxID value below.

    The third box is to print out the value of the extracted TaxID.

    Code:
    ClientInformation.Split(CChar(" "))(1).Split(CChar(Environment.NewLine))(0)
    Code:
    ClientInformation.Split(cchar(Environment.NewLine))(0).Split(cchar(":"))(1)
    Code:
    ClientInformation.Substring(ClientInformation.IndexOf("TaxID:")+"TaxID:".Length).Split(Environment.NewLine.ToCharArray)(0)
    Code:
    ClientInformation.Substring(ClientInformation.IndexOf("TaxID:")+"TaxID:".Length).Split(Environment.NewLine.ToCharArr ay)(0)
    Code:
    ClientInformation.IndexOf("TaxID: ").TrimStart("TaxID: ").Split(Environment.NewLine)(0)
    However, all of them showing the same result, that is
    Name:  error.jpg
Views: 378
Size:  23.0 KB

    The startIndex show refer to the second box which is TaxID, the length of the TaxID is fixed with 8 digits (the starting 2 digit is an alphabet and the rest is number)

    I hope master can advise on the error of the vb.net syntax.

  8. #8
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: Problem with extract string from an expression

    You can find this sign by register an account with this link https://acme-test.uipath.com/account/login
    I don't think anybody is going to Register to solve your Problem
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  9. #9
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Problem with extract string from an expression

    Have you considered regular expressions ?

    Code:
    Dim lines As String =
            "Name: Lucas Lion" & ControlChars.CrLf &
            "Age: 48" & ControlChars.CrLf &
            "TaxID: R935279" & ControlChars.CrLf &
            "Nationality: Germany"
    
    
    Dim propertyNames = New String() {"Name", "Country", "Age", "TaxID", "Nationality"}
    
    For Each element As String In propertyNames
        Dim match = Regex.Match(lines, "(?<=" & element & "\s*:\s*)\w+")
        Console.WriteLine(If(match.Success, $"{element,-12} = {match.Value}", $"{element,-12} = not found"))
    Next
    Results

    Name = Lucas
    Country = not found
    Age = 48
    TaxID = R935279
    Nationality = Germany

  10. #10

    Thread Starter
    New Member
    Join Date
    Mar 2020
    Posts
    8

    Re: Problem with extract string from an expression

    Quote Originally Posted by kareninstructor View Post
    Have you considered regular expressions ?

    Code:
    Dim lines As String =
            "Name: Lucas Lion" & ControlChars.CrLf &
            "Age: 48" & ControlChars.CrLf &
            "TaxID: R935279" & ControlChars.CrLf &
            "Nationality: Germany"
    
    
    Dim propertyNames = New String() {"Name", "Country", "Age", "TaxID", "Nationality"}
    
    For Each element As String In propertyNames
        Dim match = Regex.Match(lines, "(?<=" & element & "\s*:\s*)\w+")
        Console.WriteLine(If(match.Success, $"{element,-12} = {match.Value}", $"{element,-12} = not found"))
    Next
    Results

    Name = Lucas
    Country = not found
    Age = 48
    TaxID = R935279
    Nationality = Germany

    Thank you kareninstructor for your reply.

    the code need to be in 1 line to fit in the selector expression box which is the assign box if refer to the picture in my last thread. Sorry about the double posting.

    In the selector expression, I do not need to define the variable as dimension as we need the vb.net code to extract the TaxID value only.

  11. #11
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Problem with extract string from an expression

    Quote Originally Posted by lucasi View Post
    Thank you kareninstructor for your reply.

    the code need to be in 1 line to fit in the selector expression box which is the assign box if refer to the picture in my last thread. Sorry about the double posting.

    In the selector expression, I do not need to define the variable as dimension as we need the vb.net code to extract the TaxID value only.
    1. your image is unreadable as presented.
    2. what I provided works with and without line breaks.

    Code:
    Dim lines = "Name: Lucas Lion Age: 48 TaxID: R935279 Nationality: Germany"
    
    Dim match = Regex.Match(lines, "(?<=TaxID\s*:\s*)\w+")
    If match.Success Then
        Console.WriteLine(match.Value)
    Else
        Console.WriteLine("Failed to get Tax id")
    End If

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width