Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 05-28-2010, 10:56 AM   #1
LQ Newbie
Registered: May 2010
Posts: 2

Rep: Reputation: 0
Grep - searching for class references in C++ using regular expression

I'm trying to math all class references in a C++ file using grep with regular expression. I'm trying to know if a specific include is usuless or not, so I have to know if there is a refence in cpp.
I wrote this RE that searches for a reference from class ABCZ, but unfortunately it isn't working as I espected:

grep -E '^[^(\/\*)(\/\/)].*[^a-zA-Z]ABCZ[]*[\*\(\<:;,{& ]'

^[^(\/\*)(\/\/)] don't math comments in the begging of the line ( // or /* )
.* followed by any character
[^a-zA-Z] do not accept any caracter before the one I'm searching (like defABCZ)
[]* any white space (I can have something like ABCZ var; )
[\*\(\<:;,{& ] followed by ( * < : ; , & { (I cant get #define "ABCZ.h" or ABCZdef for example)

Well, I can get patterns like this:

class Test: public ABCZ{
class Test: public ABCZ {
class Test : public ABCZ<T>
class NameSpace::Test : public ABCZ<T>
ABCZ var = ABCZ();
ABCZ* var = new ABCZ();
ABCZ* var = new ABCZ<T>();
teste = ABCZ(var);
teste = ABCZ(var, otherVar);
teste = ABCZ(var,otherVar);
teste = ABCZ();
funct(*ABCZ av);
struct(ABCZ, BBBB, CCCC);
const ABCZ& getNot_before(void) const;

That's all right, they are all acceptable patterns. But these I'm missing, and they are all acceptable too:

ABCZ teste;
ABCZ teste; //comment
ABCZ teste; /* comment*/
ABCZ teste; /* comment*/
ABCZ teste;
ABCZ *teste;

And all these are not acceptable:
#include "ABCZ.h"
//ABCZ var = ABCZ();
ABCZdef a = ABCZdef();
DefABCZ a = DefABCZ();
ABCZdef a = ABCZdef();
/*ABCZ var = ABCZ();

I don't know if I'm missing any other patterns, if I am, please tell me =p
What am I doing wrong?
I'll be very grateful if someone answer me
Thanks in advance

Last edited by mapasserby; 05-28-2010 at 10:57 AM.
Old 05-28-2010, 11:50 AM   #2
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Strong suggestion:
1. Forget about an ad hoc regular expression
<= It's a good thing to know...
... but it's probably not the best tool for this job

2. Instead, use a tool like id-utils:;jse...OSKHWATMY32JVN

a) Install GNU id-utils (it might have different names, depending on your distro and package manager. Or you can just download it directly from GNU)

b) Run "mkid" on your source tree

c) Do a "gid" of your header

d) Voila! Problem solved!

Old 05-31-2010, 07:45 AM   #3
LQ Newbie
Registered: May 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Thank you, paulsm4, for your answer!
You had a good idea, I didnt know idutils. I tried it here, and I'm not sure if it is what I need. I forgot to say that what I really want to do it's to search for references in a specific file, to know if its includes are usuless or not. So I use doxygen to generate all files dependences and I get all includes of any files and I see if this include is being used or not.
I dont know if I'm right, but it looks like me that idutils can only search in all files specified with the mkid command, and I already know exactly each file I have to seek. So, I think it doesnt help me, because it would work just like a grep =/ or am I wrong? That's the first time I try idutils.
Any other good idea?
Thanks a lot!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to use regular expression in grep? sagarkha Linux - Newbie 6 01-23-2010 09:15 AM
grep regular expression casperdaghost Linux - Newbie 2 08-15-2009 12:01 PM
Searching for somewhat regular expression sebelk Linux - General 5 07-30-2009 02:45 PM
Searching for regular expression in command line Jean Of mArc Programming 9 08-11-2008 09:01 PM
Help with grep regular expression lamar_air Programming 3 07-21-2006 12:26 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:05 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration