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

regular expression for C if statementch
Regular expression for search name of methods in java files
Recommendation - Resource/book/pocketbook on regex in SED [closed]
Perl: composing regex thru interpolation when subexpr has capture
RegExp regular expression find & replace whole words only
Given a certain regex pattern, re-arrange and manipulate the contents of a cell
Regular expression“\\|” in strsplit
Parsing sectioned file with augeas
I need search a pattern in a header line of my file and concatenates the next line with Perl
Regular Expression statement to replace text inside two literals
RegEx pattern with not followed by
How to pull words based on positioning from a text element using Regex?
Remove C/C++, Multiline comments from a array in which file is read
Regexp match time expression (mmmm:ss)
How do I create a regex in Emacs to capture text between double square brackets?
How to set a RegExp to have a real matching judgement?

Categories

HOME
opencv
dicom
apt-get
android-activity
tcl
chronicle
robot
weblogic11g
encog
gluon
geany
ceph
jena
autofac
pouchdb
nanotime
google-cloud-sdk
ellipse
garmin
teamcity-9.0
smartcard
nxbre
nested-set-model
dsc
broker
sumifs
masstransit
mobile-center
react-redux-form
petrel
tensor
codeskulptor
sieve-of-eratosthenes
outlook-vba
executorservice
extraction
mask
amazon-vpc
essbase
polymer-cli
quicksand
pci
appdynamics
autofocus
rating-system
multisite
unityscript
python-module
webloadui
android-touch-event
iseries-navigator
sound-synthesis
ultratree
data-fitting
rsh
highslide
airplay
asp.net-webpages
balana
flexigrid
php-opencloud
clientid
delphi-10-seattle
ubiquity
netbsd
selectsinglenode
mailx
dhc
scalariform
windows-universal
antiforgerytoken
wso2as
boxapiv2
notifydatasetchanged
image-scaling
bonecp
ituneslibrary
jqgrid-php
blazeds
acts-as-taggable-on
delphi-2009
blueprint
vlfeat
transport-stream
repeating
capitalize
nsmanagedobjectmodel
angularjs-module
src
dynamic-usercontrols
eject
blackberry-cascades
vote
guvnor
mysql-backup
teamcity-7.0
media-manager
exact-synergy-enterprise
duplicate-data
nntp
hunchentoot
xoom
unitils
adbwireless
queryanalyzer
large-teams

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