Get text within a text

Quick question here. I am using a get text function in UiPath, the item I am trying to identify is a full address, but I only want to extract the state, not the entire address. When I use the function, it tries to grab the entire line of text. Is there a way to parse what text I want from that line?

Hi @Primal ,

Can you give same text. so that easy to extract state from the text.

Thanks!

My apologies; I am not sure I understand what you are saying. This is my current problem. When I try to get text, it will highlight the entire address. I only want the state within the box that it’s trying to get the text from.
image

To extract only the State from an address that you have already extracted using the UiPath Get Text activity, you can use regular expressions in UiPath. Here’s an example of how to do this:

  1. Create a new variable to hold the State value. Let’s call it “state”.
  2. Use the Assign activity to assign a regular expression pattern to the “state” variable. The regular expression pattern should match the State value in the address. For example, if the State is always abbreviated and follows a comma, you could use the following pattern:(?<=, )\w{2}This pattern matches two-word characters after a comma and a space. You can modify the pattern according to your requirements.
    e.g. on how to use with assign activity:

Assign state = System.Text.RegularExpressions.Regex.Match(address, “(?<=, )\w{2}”).Valu
e

Hi,

Another approach:

If 5 digit zip code always follows state, the following will work.

System.Text.RegularExpressions.Regex.Match(yourString,"\b[A-Z]{2}(?=\W+\d{5}\b)").Value

Regards,

1 Like

That worked, now for fine-tuning the process a little. Sometimes our addresses are Canadian, which would have 6 digits or 3, then a space and another 3. Anyway, you could say in that expression to look for the difference between US or Canadian.

Hi,

How about the following expression?

System.Text.RegularExpressions.Regex.Match(yourString,"\b[A-Z]{2}(?=\W+(\d{5,6}|\d{3}\s\d{3})\b)").Value

Regards,

That was close. I am not familiar with writing these expressions, but it does have the 3 characters then a space. Would that make a difference?
image

Hi,

As this zip includes alphabet, can you try the following?

System.Text.RegularExpressions.Regex.Match(yourString,"\b[A-Z]{2}(?=\W+(\d{5}|\w{3}\s?\w{3})\b)").Value

Regards,

Absolutely amazing. That did work. Thank you for taking the time out of your day to help me. You are much appreciated.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.