Regexp help

Discussion in 'Perl Misc' started by mike, Jul 6, 2012.

  1. mike

    mike Guest

    Hi,

    I am using the following to find matches in a file.

    open( PLUGINS, $pluginsBase ) or die "can't open $pluginsBase, $!\n";
    while (<PLUGINS>) {

    #Since list command shows both enabled and disabled versions of the plugin. Exclude disabled version in version check.
    if ( /\$id/ && !/disable/ ) {


    }

    }


    Problem I have is that when $id is equal to org.erlide it will also match contents of the file like:

    org.erlide.trace
    org.erlide.debug

    How can I match the exact:

    org.erlide

    Nothing more nothing less.

    br,

    //mike
     
    mike, Jul 6, 2012
    #1
    1. Advertising

  2. mike

    azrazer Guest

    Hi,
    would /^$id$/ do ?
    Cheers,
    azra
    Le 06/07/2012 09:20, mike a écrit :
    > Hi,
    >
    > I am using the following to find matches in a file.
    >
    > open( PLUGINS, $pluginsBase ) or die "can't open $pluginsBase, $!\n";
    > while (<PLUGINS>) {
    >
    > #Since list command shows both enabled and disabled versions of the plugin. Exclude disabled version in version check.
    > if ( /\$id/ && !/disable/ ) {
    >
    >
    > }
    >
    > }
    >
    >
    > Problem I have is that when $id is equal to org.erlide it will also match contents of the file like:
    >
    > org.erlide.trace
    > org.erlide.debug
    >
    > How can I match the exact:
    >
    > org.erlide
    >
    > Nothing more nothing less.
    >
    > br,
    >
    > //mike
    >
     
    azrazer, Jul 6, 2012
    #2
    1. Advertising

  3. mike

    mike Guest

    I tried it but I does not match at all. Here is an example of three lines that I read:

    Feature: org.eclipse.equinox.p2.user.ui 2.1.2.R37x_v20110815-1155-6-Bk8pYWZz0qUTX5I15GZWwbXkrl enabled
    Feature: org.erlide.tracing 0.2.2.201204301337 enabled
    Feature: org.erlide 0.16.0.201204301337

    we should only match org.erlide this exactly.

    br,

    //mike
     
    mike, Jul 6, 2012
    #3
  4. mike

    mike Guest

    On Friday, July 6, 2012 11:22:03 AM UTC+2, Henry Law wrote:
    > On 06/07/12 09:26, mike wrote:
    > > Feature: org.eclipse.equinox.p2.user.ui 2.1.2.R37x_v20110815-1155-6-Bk8pYWZz0qUTX5I15GZWwbXkrl enabled
    > > Feature: org.erlide.tracing 0.2.2.201204301337 enabled
    > > Feature: org.erlide 0.16.0.201204301337
    > >
    > > we should only match org.erlide this exactly.

    >
    > Oh, seeing the actual data would have helped azrazer ...
    >
    > # assuming that $id is set to "org.erlide", then
    > if ( /\s$id\s/ && !/disable/ ) {
    >
    > Or even more explicitly, assuming that all your data is as you've shown
    > if ( /^Feature: $id\s/ && !/disable/ ) {
    >
    > BTW you'd coded /\$id/; that will match the string "$id", not the value
    > of the variable $id;
    >
    > --
    >
    > Henry Law Manchester, England


    Thanks that worked out fine. Sorry azrazer for leaving out essential information.

    //mike
     
    mike, Jul 6, 2012
    #4
  5. Henry Law <> wrote:
    >On 06/07/12 09:26, mike wrote:
    >> Feature: org.eclipse.equinox.p2.user.ui 2.1.2.R37x_v20110815-1155-6-Bk8pYWZz0qUTX5I15GZWwbXkrl enabled
    >> Feature: org.erlide.tracing 0.2.2.201204301337 enabled
    >> Feature: org.erlide 0.16.0.201204301337
    >>
    >> we should only match org.erlide this exactly.


    No, you don't want to match, you want to test for equality.

    >Oh, seeing the actual data would have helped azrazer ...
    >
    ># assuming that $id is set to "org.erlide", then
    >if ( /\s$id\s/ && !/disable/ ) {


    Yeah, right. Why do people, including the OP, insist on using REs for
    comparing text? And you fell into the same trap, too.

    Your suggestion will match e.g.
    orgXeclipse
    too, and I don't think this is what the OP wants.

    Yes, this again can be patched by using quotemeta, but why?
    If you want to check two strings for equality then just do so instead of
    fumbling around with REs:

    (undef, $domain, $code, $status) = split($_, ' ');
    if ($domain == $id and $status!='disable') {
    ....
    }

    jue
     
    Jürgen Exner, Jul 6, 2012
    #5
  6. mike

    Jim Gibson Guest

    In article <>, J¸rgen Exner
    <> wrote:

    > If you want to check two strings for equality then just do so instead of
    > fumbling around with REs:
    >
    > (undef, $domain, $code, $status) = split($_, ' ');
    > if ($domain == $id and $status!='disable') {
    > ....
    > }


    But please do use the string comparison operator:

    if ($domain eq $id ...

    --
    Jim Gibson
     
    Jim Gibson, Jul 6, 2012
    #6
  7. Jim Gibson <> wrote:
    >In article <>, J¸rgen Exner
    ><> wrote:
    >
    >> If you want to check two strings for equality then just do so instead of
    >> fumbling around with REs:
    >>
    >> (undef, $domain, $code, $status) = split($_, ' ');
    >> if ($domain == $id and $status!='disable') {
    >> ....
    >> }

    >
    >But please do use the string comparison operator:
    >
    > if ($domain eq $id ...


    Good idea ;-)

    jue
     
    Jürgen Exner, Jul 7, 2012
    #7
  8. Ben Morrow <> wrote:
    >Quoth J?Exner <>:
    >>
    >> (undef, $domain, $code, $status) = split($_, ' ');
    >> if ($domain == $id and $status!='disable') {

    > ^^ ^^
    >Um, you were saying something about falling into traps?


    Ummm, yeah, that happens when coding in several similar but not quite
    identical languages at the same time.

    Thanks for catching it.

    jue
     
    Jürgen Exner, Jul 7, 2012
    #8
    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. Greg Hurrell
    Replies:
    4
    Views:
    177
    James Edward Gray II
    Feb 14, 2007
  2. Mikel Lindsaar
    Replies:
    0
    Views:
    542
    Mikel Lindsaar
    Mar 31, 2008
  3. Joao Silva
    Replies:
    16
    Views:
    404
    7stud --
    Aug 21, 2009
  4. Uldis  Bojars
    Replies:
    2
    Views:
    213
    Janwillem Borleffs
    Dec 17, 2006
  5. Matìj Cepl

    new RegExp().test() or just RegExp().test()

    Matìj Cepl, Nov 24, 2009, in forum: Javascript
    Replies:
    3
    Views:
    202
    Matěj Cepl
    Nov 24, 2009
Loading...

Share This Page