regex


Removing leading, trailing and multiple spaces within a string


I would like to remove all leading and trailing spaces. As well as replace multiple spaces with a single space within a string, so that all words in a string are separated exactly by single space.
I could achieve this using following two iteration of regex and looking for single regex solution.
s/^\s+|\s+$//g
s/\s+/ /g
Sample Input:
word1 word2 word3 word4
Desired Output:
word1 word2 word3 word4
It would be appreciable if you could help me to solve this.
You can use something like:
s/^\s+|\s+$|\s+(?=\s)//g
\s+(?=\s) will match all the spaces in the middle of the string and leave one.
Using awk
echo " word1 word2 word3 word4 " | awk '{$1=$1}1'
word1 word2 word3 word4
This $1=$1 is a trick to concentrate everything.
You can even use
awk '$1=$1' file
But if first field is 0 or 0.0 it will fail
This might work for you (GNU sed):
sed -r 's/((^)\s*(\S))|((\S)\s*($))|(\s)\s*/\2\3\5\6\7/g' file
or simply:
sed -r 's/(^\s*(\S))|((\S)\s*$)|(\s)\s*/\2\4\5/g file
In Javascript, the string prototype has two methods that can manage this:
str.trim().replace(/\s+/g, ' ')
str.trim() will remove leading and trailing spaces
str.replace(regex, replacement) will return a new string (nondestructive to original str) where regex will be compared against the provided string and the first instance of a match will be replaced by replacement, then the whole new string is returned.
Important thing to note: the first param of .replace should not be encapsulated with quotes. Regex is delimited with slashes (/regex/) and then g is appended to mean replace globally (every matched instance) rather than just replacing the first or next instance based on lastIndex (which is initially 0, giving the first instance). You can read more about lastIndex and everything I've mentioned at second link provided.
example:
var str = ' 1 2 3 4 '
function trimReplace(str){
newStr = str.trim().replace(/\s+/g, ' ');
console.log(newStr);
}
trimReplace(str)
Try this in your console:
' 1 2 3 4 '.trim().replace(/\s+/g, ' ')
"1 2 3 4"
_
regex: kleene operators will help you understand the regex used to match multiple spaces
regex: helpful guide on regex and /g flag
Google: MDN string.protoype.trim()
Google: MDN string.prototype.replace()
If you are on UNIX, you could take advantage of the shell's Word-splitting. Bash example using command substitution below
STR=" word1 word2 word3 word4 "
z=$(echo $STR)
echo "$z"
word1 word2 word3 word4

Related Links

Findstr - Return only a regex match
Converting VB function to Regex?
how to put text after a line beginning with http?
How to find a regex in re2 to search between strings?
How to combine two lists in one list? using notepad++ regex? [duplicate]
Scala - Explanation for regex statement
Python Regular Expression Multiple Groups
Using regexp in Matlab to find specific file from beginning and end components of filename
regular expression to check ends with a digit
Regex to capture string until another string is encountered
Google apps script, getBody() from Gmail, regex \n
How can I do a search across multilines in Delphi's regex
strip a regex character match from a string
Replacement within the matched string with sed
AHK - How to use dynamic hotstring with a barcode scanner
Fuzzy match on google sheets

Categories

HOME
ssl
embedded-linux
ffmpeg
vr
module
varnish
apache-pig
datetime
android-dialer
android-bluetooth
telerik
regression
android-vision
command-line-interface
scrollbar
plc
lookup-tables
snort
formio
teamcity-9.0
jscodeshift
rtp
ghost-blog
powershell-v2.0
eonasdan-datetimepicker
centos6
esri
unsigned-integer
listbox
paket
apscheduler
operator-keyword
silverlight-5.0
stomp
scom
swipe
pvlib
complex-networks
machine-language
stackexchange.exceptional
tweenmax
hamcrest
spring-data-cassandra
jdom-2
powerbuilder-conversion
extjs4
essbase
automata-theory
mdanalysis
ulimit
tabview
angularjs-routing
sweet.js
pci
scoring
asyncsocket
android-gps
jasmine2.0
ternary
catalina
liclipse
gwt-material-design
cakephp-2.3
evaluate
ckcontainer
tealeaf
ellipsis
activity-diagram
selenium2library
groupbox
flotr2
pakyow
jni4net
egl
linqtocsv
wand
ospf
wchar-t
hjson
monkeyrunner
kindlegen
xsb
devenv
newsql
twitter-follow
nolio
django-supervisor
master-theorem
keymapping
confirmation
nmea
nsusernotificationcenter
broadcom
repeating
jmdns
vim-fugitive
intel-8080
springboard
machine.config
xoom
unitils
excel-web-query
68hc11
graceful-degradation
roguelike

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App