romanK
romanK

Reputation: 63

Remove whitespace before or after a character with regex

I new in regex and i want to find a good solution for replacing whitespace before or after the / char in my sub string.

I have got string like

"Path01 /Some folder/ folder (2)"

i checked regex

@"\s?()\s?"

but this incorrect for me. I must get in output

Path01/Some folder/folder (2)

Can you help me?

Thanks!

Upvotes: 4

Views: 5507

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626690

You may use

@"\s*/\s*"

and replace with /.

See the regex demo

The pattern matches zero or more (*) whitespace chars (\s), then a / and then again 0+ whitespace chars.

C#:

var result = Regex.Replace(s, @"\s*/\s*", "/");

Upvotes: 5

Related Questions