Question: I am trying to analyse some third party code and I am interested in all arrays that are declared+defined in the given source (.c and .h). This is interesting to know if some memory is wasted when porting to an embedded system.

I can assume that only standard types are used, i.e. char, int, long, float, double.

Here is an example of what I want to find:

```
char message[100];
int tag[MY_PERSONAL_TAG_SIZE];
double vald[2]   ={1.0,1.1}; 

```

I have tried the following with the great support of a colleague but it gets some false positives:

```
egrep -e '(char|int|long|short|float|double)[ \t]*[^ \t)]+[ \t]*\[[^ \t]+\]' *

```

This also finds (which it shouldn't):

```
wordTable  = &intTable[0];
MyFunction (MyPointer* foo, int *bar, int code[4], int add[4], char *Mystring[4]) {

```

I guess the regex also finds some other false positives and might miss some definitions it should find. So I would be happy to hear any suggestions. Thanks.

Answer: 
This will require some regex wizardry but it **will not be reliable enough**. You should use a proper C parser for such tasks.

But still, if you want to do it quick&dirty, here's a Perl (or Perl-compatible) regex that does the trick (it needs to be used with the `m` and `s` modifiers):

```
(?:\(.*\)|\/\*.*?\*\/)(*SKIP)(?!)|(?:^|[;,])\s*\K(?:char|int|long|short|float|double)(?:\s*\**\s*\w+\s*\[\w*\]\s*[=;,])+

```

Demo: <http://regex101.com/r/hY6sV2/3>

It ignores everything in comments or parentheses, and tries to find an array declaration at the start of a line or after a colon or semicolon.
Try:

```
(?:char|int|long|short|float|double)(?:\s+.*?)?\[.*?\].*?;

```

This regex matches the requirements, if:

* The array is of the type: char, int, long, short, float, double
* The Array declaration ends with ;