arunhorne has asked for the wisdom of the Perl Monks concerning the following question:

Sorry, this is probably a really dumb question but I have this file containing a set of equations. I'm trying to standardise some data sets and so have written a little script to search it using a regex. I want to find all the occurrences of ='s that do not have whitespace on either side. I can do it using the three regular expressions:

m/\S=\S/g m/ =\S/g m/\S= /g

However when I try to combine all of these into one regex as follows it stops working:

m/(\S=\S)|( =\S)|(\S= )/g

I'm sure its a silly error but what am I doing wrong?

Replies are listed 'Best First'.
(jeffa) Re: Rewriting Three Regexes as One
by jeffa (Bishop) on May 04, 2002 at 14:55 UTC
    How about m/\S+=\S+/ instead of alteration?
    while (<DATA>) { print if /\S+=\S+/; } __DATA__ $foo = 'hello'; $bar= 'hello'; $baz ='hello'; $qux='hello';
    UPDATE:
    Hope i am not second guessing myself, but i believe i am wrong here. You want only the bar, baz, and qux lines - correct? Looks like your answer is the right way. Sorry, that's what i get for posting before morning coffee. ;)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Rewriting Three Regexes as One
by arunhorne (Pilgrim) on May 04, 2002 at 11:27 UTC

    OK, I found the answer myself. Just in case anyone else ever has the same problem... its because I included brackets!! The following will work fine. I felt they improved readability, but this is no good at the expense of it not working ;D

    m/\S=\S| =\S|\S= /g

    Sorry people.

      m/\S=\S| =\S|\S= /g

      Since you are concerned about readability, here is what I would prefer ... not that everyone thinks like me, but at least you have one more option :-)

      # Bad: m/[\S ]=[\S ]/g m/(?!: = ).=./gs

      Update:I should not post this early ... I misread the specs. Without doing capturing braces (and you don't want that, I gather), I can do no better than your version.

      Update 2:Added the negative lookahead version.

      The Sidhekin
      print "Just another Perl ${\(trickster and hacker)},"