Sublime Forum

Regex search single line , remove leading spaces

#1

I am confused.

This is 2181 on windows XP

I want to search and remove leading spaces by using smtg like “^\s+(\w.+)$”, “$1”

But Sublime always match greedy multi lines and remove line break too and not spaces only?

Example (dots indicate space here):

[code]Start

Line number one

…Second line here

…Third line[/code]

gives me:

Start Line number one Second line here Third line

instead of wanted:

[code]Start

Line number one

Second line here

Third line[/code]

What do i wrong?

.

0 Likes

#2

Do this:

^^\S\r\n]+(\w.+)$

That should give you what you want.

\s was including \r\n, by saying do not include things other than whitespace (\S) and do not include newlines (\n) and carriage returns (\r), you are getting left with just tabs and spaces, or what you thought you were getting with \s. \s includes line breaks.

Edit
Alternatively you could just say, only tabs and spaces.

^ \t]+(\w.+)$

Both work just fine. Not sure why my brain picked the first over this. My brain seems to gravitate to the more obscure solution first…oh well. :smile:

0 Likes

#3

Thank you.

Your regex works of course.
^ \t]+?(\w.+)

And true is, “\s includes line breaks.”
And SublimeText does an good job here, i like this.

But why didn’t SublimeText have the option to let the** ^ caret** match at start of (an single) line ?

From regular-expressions.info/reference.html

At least “^\s+?(\w.+)” should match non-greedy an single line only, isn’t it?.

But i think SublimeText behave like

Then i think the flag (?-m) should disable this, but that doesn’t work.

What do others think about this behaviour? Am i only wrecked by the weak RE implementation of other editors :laughing: ?

.

0 Likes