Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

my substitution problem:

by Madams (Monk)
on Feb 13, 2001 at 07:35 UTC ( #58069=perlquestion: print w/replies, xml ) Need Help??
Madams has asked for the wisdom of the Perl Monks concerning the following question:

I have a string,and a substitution:
it should result in:
   'b^10+s' NOT 'b^1s' !
it works if the number to the right of the "^" is less than 10 but not if greater.
i want to strip all numbers followed by a "+" (including the "+") but NOT if they are preceeded by a "^".

what should i do? (regexi drive mike crazy...(is that the proper plural?)

Replies are listed 'Best First'.
Re (tilly) 1: my substitution problem:
by tilly (Archbishop) on Feb 13, 2001 at 07:49 UTC
    Regexen problems? Well the rule is that it will work really really hard to find a match. So figure out why it is finding the unwanted match and deny it.

    Well on the second substitution it is clearly able to match "0+" after a 1. Well does that fit the rule? Why yes, the preceeding "1" is not "^", so it can match What to do? Why deny it a digit there. That gives us:

    s/(?<![\^\d])(?:\d+\+)//g; # ^^^^^^ <- was just \^ before
    which does what you asked.
      Hey tilly!
      thanks (for the fix and the plural i LIKE regexen as a word), i think i was getting google eyed trying to figure that out.. i was relatively sure it involved my not specifying the negassert correctly but i just continually woofed it! regexen are one way to kill your eyesight ;)

      after a while they look like /{][P)(*^8(*(/ over and over :)
      p.s. still like the multfor "loop" you clued me in on (still studying it cause it's a cool hack!)

      Mucho gracias!
OGRE strikes again! Re: my substitution problem:
by japhy (Canon) on Feb 13, 2001 at 20:00 UTC
    In an effort to shamelessly plug my modules, I produce the output of the OGRE:
    NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- (?<! look behind to see if there is not: ---------------------------------------------------------------------- \^ '^' ---------------------------------------------------------------------- ) end of look-behind ---------------------------------------------------------------------- (?: group, but do not capture: ---------------------------------------------------------------------- \d+ digits (0-9) (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- \+ '+' ---------------------------------------------------------------------- ) end of grouping ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------
    As tilly pointed out, the problem is that in the string "^123", "123" isn't a valid match, but "23" is.

    japhy -- Perl and Regex Hacker

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://58069]
Approved by root
and the grasshoppers chirp...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2018-05-21 23:43 GMT
Find Nodes?
    Voting Booth?