How to remove a double-space from a variable thats used in Regex?

Hey guys

I have a variable called in_SenderName that has the name of the .txt file that i use to call in my Matches activity. But the regex expression is not working because of a little annoying thing as a double-space between the text inside my In_SenderName variable.
I want to get rid of that so it has the exact same output as the file name (with single space).

This is my text input:

The name of the file Ibra UiPath (see the number 1 in the attached picture) is my variable name that i get from the file.
The number 2 is the name (with the double space) i use to retrieve the number 3 (with blue).

Untitled

This is my Regex expression to retrieve UiPath Junior:

  • So my question is:
    How do i remove the double-space inside my In_SenderName variable?

Thank you.

Try this, it should address your concern

Hi, Ibra. The description seems a bit ambiguous. Tell me if I’ve got things correctly.

Your variable name: in_SenderName.
This has “Ibra UiPath.txt” as its value (there are double spaces in between ‘Ibra’ and ‘UiPath’).

Queries: for Matches activity, what is your:

  1. Input
  2. Regex used
  3. Output received
  4. Desired Output

Please provide the answers in text and not as images so that i can easily copy it and test it.

I tried it with these expression and it didnt work:

“(?<=[\n\r]+?”+ in_senderName.Replace(in_senderName, " {2,}“, " “) +”[\n\r]+?)\w.*”

“(?<=[\n\r]+?”+ in_senderName.Replace(" {2,}“, " “) +”[\n\r]+?)\w.*”

But i tried with the following syntax (hardcoded and its not reliable but still worked):

“(?<=[\n\r]+?”+ in_senderName.Replace(" “, " “) +”[\n\r]+?)\w.*” → I replaced the double space with single space

Can you correct me please? I dont want to hardcode it.

It has only Ibra UiPath as value (not followed by .txt).

I use that variable to get the string UiPath Junior (because the variable is represented both in the .txt file and inside the .txt file)

  1. Input: Ibra UiPath (variable name: In_SenderName)
  2. Regex:

(?<=[\n\r]+?“+ in_senderName +”[\n\r]+?)\w.*

  1. Output: NULL
  2. Desired output: UiPath Junior

One question, where is ‘Junior’ in this? hardcoded?

‘UiPath Junior’ is the string im trying to extract

If you are trying to match:

Ibra[space]UiPath with Ibra[space][space]UiPath

then you can just replace all the spaces and compare by using string replace.

In that case, your input won’t be “Ibra UiPath”. Input will be the text inside the text file.

The problem is you’re trying to use your variable as the key or anchor here. But since there are two spaces in between the Ibra and Uipath inside the text file (not the variable) it won’t match. But since you’re absolutely sure that the file name is going to come right before the string you need, do this:

Regex.replace(in_SenderName," “,” +")

Use the above instead of in_SenderName inside your regex:

(?<=[\n\r]+?“+ Regex.replace(in_SenderName,” “,” +“) +”[\n\r]+?)\w.*

This should work PROVIDED your regex is correct.

Let me know if this worked for you.

RegexExtractSpecificText.zip (2.8 KB)

Here. I’ve made it easier for you. See if this helps and let me know.

Worked, thank you!