Hugo C.
Hugo C.

Reputation: 53

Remove first whitespace in regex

Im newbie in regex so I need help how to remove first whitespace in regex in each row in notepad++?

Example :
 191.341.411.314 80
 191.341.411.315 80
 191.341.411.316 80
 191.341.411.317 80

and I need this
191.341.411.314 80
191.341.411.315 80
191.341.411.316 80
191.341.411.317 80

list include +1000 lines what is a best solution??

Upvotes: 3

Views: 12263

Answers (5)

aelor
aelor

Reputation: 11116

After your update its clear that you have a file with these lines.

In that case its better to do something like this :

the awk way

awk '{print $1" "$2}' text.txt

where text.txt will be the file containing your initial text. you can write out the result in a different file using this:

awk '{print $1" "$2}' text.txt > finaltext.txt

or the perl way

perl -pe 's/^\s//g' text

=========

var str = " 191.341.411.314 80"; //with a space at beginning
console.log(str);
var res = str.replace(/^\s?/, '');
console.log(res);

try this in your firebug console.

Upvotes: 0

Jaykumar Patel
Jaykumar Patel

Reputation: 27614

Check this Demo jsFiddle

RegExp

/^\s/         // remove only first whitespace

/\s/          // remove first whitespace entire string
/ /           // remove first whitespace entire string

Code

var finalUrl = " 191.341.411.314 80";

console.log(finalUrl);                                // Before
console.log(finalUrl.replace(/^\s/ , ''));            // After

Result

 191.341.411.314 80
191.341.411.314 80 

Hope this help you!

Upvotes: 0

Blekit
Blekit

Reputation: 663

(?<=\s).* will select everything that follows a single whitespace character. However, it will not match to the strings that doesn't start with a whitespace.

Anyway, wouldn't simply trimming the string solve your problem? For this use case regex seems to be a bit of overkill to me.

Upvotes: 0

stema
stema

Reputation: 92986

You just need to anchor your regex to the start of the string

^\s

and replace with nothing. \s is a shorthand character class for whitespace

See it here on Regexr

Upvotes: 10

Severin
Severin

Reputation: 8588

To remove the first whitespace you have to gsub the string you are having like so:

" 191.341.411.314 80".gsub(/^\p{Space}/, '')

Upvotes: 0

Related Questions