RegEx: Looking for something that is NOT there

2

2biased

Hi,

Not sure that this the right group but as Perl does a lot of RegEx I
thought I might give it a shot.

Consider a large source code project with a lot lines like

const unsigned int a = 0;
const b = 1;
const bool c = FALSE;
const CString d = "10";

etc. As you see the 2nd line has no type specifier which was defaulted
to int in the previous compiler version. The new version requires the
"int" explicitly so I have to find all occurrences for

const <variablename> = <somevalue>;

A expression like

(const)(\s+)(unsigned|int|bool|CString)(.*)(;{1,1})

gives me lines 1, 3 and 4 as result as expected. But how do I get only
line 2 as result when I only know that it should not contain various
type names (bool, int, float, unsigned ...)?

I tried this for several hours now but didn't get a working solution.
Any ideas on how to do that?

Thanks a lot for your time.
Regards
 
X

xhoster

2biased said:
Hi,

Not sure that this the right group but as Perl does a lot of RegEx I
thought I might give it a shot.

This is the right group only if you plan on using Perl to run your RegExes.
If you are not going to use Perl, this is not the right group. For
example, if we help someone make a Perl Regex to do what they want, then
they complain that the Perl Regex doesn't run in <something other than
Perl>, this really bugs us.
Consider a large source code project with a lot lines like

const unsigned int a = 0;
const b = 1;
const bool c = FALSE;
const CString d = "10";

etc. As you see the 2nd line has no type specifier which was defaulted
to int in the previous compiler version. The new version requires the
"int" explicitly so I have to find all occurrences for

const <variablename> = <somevalue>;

A expression like

(const)(\s+)(unsigned|int|bool|CString)(.*)(;{1,1})

gives me lines 1, 3 and 4 as result as expected. But how do I get only
line 2 as result when I only know that it should not contain various
type names (bool, int, float, unsigned ...)?

I would not try to do it in a single regex, but rather a regex with some
code to go with it.

my %allowed; @allowed{qw/unsigned int bool CString/}=();

while (<>) {
if (/(const)\s+(\S+)(.*)(;{1,1})/ and not exists $allowed{$2} ) {
# do whatever you want to do;
}
};


Xho
 
J

John Bokma

2biased said:
const unsigned int a = 0;
const b = 1;
const bool c = FALSE;
const CString d = "10";
[..]

gives me lines 1, 3 and 4 as result as expected. But how do I get only
line 2 as result when I only know that it should not contain various
type names (bool, int, float, unsigned ...)?

I tried this for several hours now but didn't get a working solution.
Any ideas on how to do that?

/const\s+\S+\s*=/

const followed by one or more whitespace followed by at least one non-
whitespace followed optionally by whitespace, followed by the assignment
operator.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top