Vime regex does not match spaces in character class

I use vim to find and replace with this command:

%s/lambda\s*{\([\n\s\S]\)*//gc

I try to match all words, end and space characters after { . For example, this entire line should match:

  lambda { FactoryGirl.create ... 

Instead, it matches only the new line and the spaces before FactoryGirl . I tried manually replacing all the spaces earlier, just in case there were tabs instead of bones instead. Can someone explain why this is not working?

+3
source share
2 answers

\s - atom for spaces; \n , although it looks similar, it is syntactically an escape sequence for a newline. Inside the atom of the collection [...] you cannot include other atoms, only characters (including some special ones, such as \n . From :help /[] :

  • The following translations are accepted when the 'l' flag is not included in "cpoptions" {not in Vi}:
 \e <Esc> \t <Tab> \r <CR> (NOT end-of-line!) \b <BS> \n line break, see above |/[\n]| \d123 decimal number of character \o40 octal number of character up to 0377 \x20 hexadecimal number of character up to 0xff \u20AC hex. number of multibyte character up to 0xffff \U1234 hex. number of multibyte character up to 0xffffffff 

NOTE. The other backslash codes mentioned above do not work internally [!]

So, either specify whitespace literally [ \t\n...] , use the appropriate expression for the character class [[:space:]...] , or combine the atom with the collection through logical or \%(\s\|[...]\) .

+11
source

Vim interprets characters within character classes [ ... ] differently. This is not literal, as this regular expression will not fully match lambda {sss or lambda {\\\ . What \s and \s interpreted as ... I still cannot explain.

However, I was able to achieve almost what I wanted:

 %s/lambda\s*{\([\n a-zA-z]\)*//gc 

This ignores the punctuation I wanted. This works, but is dangerous:

 %s/lambda\s*{\([\n a-zA-z]\|.\)*//gc 

Since adding a character after the last character, such as } , causes vim to hang during globbing. So my solution was to add the punctuation that I need to the character class.

+1
source

Source: https://habr.com/ru/post/989507/


All Articles