grep - Why does this regex not work for matching C integer declarations? -


i'm using following regular expression match c integer declarations without initialization. integer might unsigned:

^(unsigned[[:space:]]+|^$)int[[:space:]]+[a-za-z0-9_]+;$ 

the first part matches unsigned followed number of spaces, or nothing. second part matches int followed spaces, plus variable name , semicolon.

unfortunately, seems not match integer declaration doesn't have unsigned in it. doing wrong? ^$ in (...|...) pattern expect (match empty string)? google , regex manual isn't helping.

try this:

^[[:space:]]*(unsigned[[:space:]]+)?int[[:space:]]+[a-za-z0-9_]+;[[:space:]]*$ 

to make group optional, put ? after it. ^$ doesn't match empty string in middle of match, matches entirely empty string -- ^ matches beginning of string, , $ matches end of string.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -