[QUES] Remove multiple white spaces and empty lines in a string?

Hello, I’m like 2 months new to UIPath and am still going through Associate training.
I have this sudden problem that I cannot solve. Also wanted to initiate my engagement in forum too.

So I have a random pattern of document extracted every time that has spaces in the beginning, has blank rows, or just random white dashes. (Data below)

All the extracted data follow the same pattern more or less (give or take a few new lines).

My target is : To remove all unnecessary white spaces (Not between two words of a sentence) and to arrange them in their own lines for the message box.

"

Joy

Roger

12

  • 33
    I am happy
    "

So this is my random string (including all the spaces) bounded with “”. My target is something like this →

“Joy
Roger
12
+
33
I am happy”

Note; if I use the replace methods, the expected gap between “I am happy” is turning out to be “Iamhappy”.
It’d be nice if it was possible without regex because I’m still practicing there. But if there’s a simple regex code too to remove the spaces, please let me know. (My attempts on regex couldn’t fully solve the issue)

I’ve browsed the forum for hours and either the methods don’t work in my sample, or too complicated for me.

A screenshot with current progress is attached. I used →

Removes the whitespaces -
Test_Data.Replace(" ", [String].Empty)

Removes the extra empty lines -
String.Join(vbCrLf, Test_Data.Split(Environment.NewLine.ToArray, StringSplitOptions.RemoveEmptyEntries))

Hi @fazewalker

Can you try this

Test_Data =

System.Text.RegularExpressions.Regex.Replace(Test_Data, "[\r?\n]{2,}", vbcrlf).Trim

HI,

How about the following?

System.Text.RegularExpressions.Regex.Replace(yourString,"^\s+","",System.Text.RegularExpressions.RegexOptions.Multiline).Trim

Sequence.xaml (5.2 KB)

Regards,

1 Like

Thank you for your reply. However, it is only removing a few additional spaces.

An output sample is attached in this screenshot →
1212

Thank you this worked like a charm.

I’ll try to understand the whole command by myself and/or reverse engineer it to figure out.

If you have any suggestion on how to get good at this, or how I’d be able to do the same by myself, please let me know. A kind explanation would be great too if you have time.

I’ll mark this as solution in a while, thank you!

We can use regex101.com for initial explanations:
grafik

1 Like

Hi,

I think the important thing is to accumulate experience regarding regex. The more we write regex pattern or see and understand pattern which is written by others in various developments, the better we can write it. And regex site will also help us to understand it as @ppr shows.

Regards,

1 Like

Thank you both for the help.
Glad to be in the forums and hope to learn more!
@ppr @Yoichi

@fazewalker
For regex learning and knowledge checking also have a look here:

1 Like

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