Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 166
  • Last Modified:

Regex help !!!

Hi you experts.

Suppose i have the following string : "         99       MyData                "
I need a regular expression that will trim all spaces(spaces and tabs) and only puts one space between them like this : "99 MyData" .

Thank you all in advance.
0
adktd
Asked:
adktd
1 Solution
 
ddrudikCommented:
I would first "trim" the string with one of the built-in functions in whichever string library you have loaded, then regex replace \s+ with \s to condense the multiple consecutive spaces into individual spaces.
0
 
adktdAuthor Commented:
Can you provide me with an example so i can test it?
0
 
josgoodCommented:
        string s = "         99       MyData                ";
         string result = Regex.Replace(s,@"(^\s+)|(\s+$)","");
         result = Regex.Replace(result,@"\s+"," ");

The first replace removes spaces at the start and end of the line.

The second replaces multiple consecutive spaces with a single space.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now