简体   繁体   中英

Is there any way to exclude the last occurence of a character from a regex group with grep

I want to write a regex that matches the following lines:

int func ( int argument, char* str)
for( int i = 0; i < 25; i++)
if( true )

but not thoses ones:

int func (int argument char* str)
for(int i = 0; i i < 25; i++)
if(true)

In litteral, I want to match lines in which there is any whitespace following an opening parenthesis or any whitespace preceding a closing parenthesis.

I started by writing the following regex:

grep -E '[^\(]\( [^ ] \)'

But it is not working, since there may be valid spaces between the parenthesis.

My question is: Is there any way to exclude the last occurence of a character from a regex group?

In litteral, I want to match lines in which there is any whitespace following an opening parenthesis or any whitespace preceding a closing parenthesis.

You may use this grep with an alternation:

grep '([[:blank:]]\|[[:blank:]])' file
  • In default grep regex mode (BRE) there is no need to escape ( and )
  • [[:blank:]] matches a space or a tab character
  • You need to escape | in BRE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM