RegEx: Looking for something that is NOT there

Discussion in 'Perl Misc' started by 2biased, Dec 11, 2006.

  1. 2biased

    2biased Guest

    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
     
    2biased, Dec 11, 2006
    #1
    1. Advertising

  2. 2biased

    Guest

    "2biased" <> wrote:
    > 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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Dec 11, 2006
    #2
    1. Advertising

  3. 2biased

    John Bokma Guest

    "2biased" <> wrote:

    > 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.

    --
    John Experienced Perl programmer: http://castleamber.com/

    Perl help, tutorials, and examples: http://johnbokma.com/perl/
     
    John Bokma, Dec 11, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    4
    Views:
    482
    Guest
    Oct 13, 2004
  2. Pekka Järvinen
    Replies:
    2
    Views:
    667
    Richard Tobin
    Apr 29, 2008
  3. Replies:
    3
    Views:
    769
    Reedick, Andrew
    Jul 1, 2008
  4. Replies:
    4
    Views:
    220
    Tad McClellan
    Jun 1, 2007
  5. Replies:
    9
    Views:
    161
Loading...

Share This Page