.NET Framework Bookmark and Share   
 index > Regular Expressions > Replace multiple spaces
 

Replace multiple spaces

Hi, Im trying to use regular expressions in VB.Net to remove spaces at the start of a new line and end of a line, im not sure what the search syntax would be and also what the replace syntax would be? Can anyone help?

i.e.

from

Hello

to

Hello

and also multiple spaces between to words to a single space unless they are enclosed between speech marks

from

Hello World & " do not replace extra spaces here" & remove extra spaces here

to

HelloWorld & " do not replace extra spaces here" & removeextraspaces here

SunnyD
Note in the replace there is a space such as "${Text} " ....
Input TextThe "brown fox" jumped over...
Regular Expression

(?(\x22)#Ifitstartswithaquote,keepitasis
(?<Text>\x22[^\x22]*\x22)#ExtractittoNamedcapturegroup
|
(?<Text>[^\s]*)#ExtracttoNCG
)
(\s*)#Matchspaces/tabstoeleminatethem.

Replace Pattern

${Text}


Replacement ResultThe"brownfox"jumpedover...
Regex Options
Compile ECMA Single Line Multi Line Ignore White Space Explicit Capture Ignore Case Right To Left Culture Invariant
�/font>

Regex Info Generated by the Regex Responder V1.0

OmegaMan
Why don't you simply use the Trim method that exists on string?
timvw

That wouldn't quite work as it would only removal spacing at the start and end of the line and not multiple spacing in the line.

Thank you.

SunnyD
You may want to give a better example of what you're expecting then, because i thought your example exactly wanted to remove spaces at the begin and end (and not in the 'between')...

Regex.Replace(input, @"^\s+(?<between>.*?)\s+$", "${between}") seems to do exactly the same...
timvw

Hi Tim,

Sorry example was not clear, Im looking to remove any multiple spaces throughout the sentence unless they are enclosed within speech marks.

Therefore in the following sentence I am looking to remove the extra spaces between "brown" and "fox" and "fox" and "jumped"

The brown fox jumped over....

however if enclosed in speech marks as as per the next example then I only want to remove extra line spaces between "fox" and "jumped"

The "brown fox" jumped over...

Hope this is clearer.

Thank you.

Sunny

SunnyD
Note in the replace there is a space such as "${Text} " ....
Input TextThe "brown fox" jumped over...
Regular Expression

(?(\x22)#Ifitstartswithaquote,keepitasis
(?<Text>\x22[^\x22]*\x22)#ExtractittoNamedcapturegroup
|
(?<Text>[^\s]*)#ExtracttoNCG
)
(\s*)#Matchspaces/tabstoeleminatethem.

Replace Pattern

${Text}


Replacement ResultThe"brownfox"jumpedover...
Regex Options
Compile ECMA Single Line Multi Line Ignore White Space Explicit Capture Ignore Case Right To Left Culture Invariant
�/font>

Regex Info Generated by the Regex Responder V1.0

OmegaMan

That's excellent works a treat many thanks.

SunnyD

You can use google to search for other answers

Custom Search

More Threads

• Regex.Replace Method (String, String, MatchEvaluator, RegexOptions) sometime works sometime not
• Searching Value of name attribute from HTML tags like input,select etc.
• Regular expresison for ../
• Regular Expression for URL / IP with Port Number
• need regular expression to not include strings contains a special charchters
• Validating Combobox
• Capture integers in string
• match all words not equal to some pattern
• Help me to create regexp for deselect some part in a line
• Simple Regular Expression