Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^4: How to avoid an alphabet and integer next to it in a string?

by moritz (Cardinal)
on Mar 21, 2014 at 18:20 UTC ( #1079323=note: print w/ replies, xml ) Need Help??


in reply to Re^3: How to avoid an alphabet and integer next to it in a string?
in thread How to avoid an alphabet and integer next to it in a string?

The Problem with s/H\d*//g is that it will also remove the H from HgS (ok,, not organic chemistry, but you get the point).

One way to avoid that is to use a negative look-ahead:

s/H(?![a-z])\d*//</c> <p>Or if you prefer the Unicode-y approach:</p> <code>s/H(?!\p{Ll})\d*//


Comment on Re^4: How to avoid an alphabet and integer next to it in a string?
Select or Download Code
Re^5: How to avoid an alphabet and integer next to it in a string?
by hazylife (Monk) on Mar 21, 2014 at 18:29 UTC
    will also remove the H from HgS
    Yes, you're right, I stand corrected.
Re^5: How to avoid an alphabet and integer next to it in a string?
by piscean (Acolyte) on Mar 21, 2014 at 18:38 UTC
    The first code says Forbidden chars again. Second one, Unicode -y approach? I am not familiar with that. Could you help me? How should the input be in an unicode -y approach?
      The first code says Forbidden chars again.
      So show the input and output, quit making us guess (and when I say 'output' I mean the new string, not the number you're trying to calculate).

      As far as which approach, don't worry about it. Just pick one (as long as it works).

        Input was
        my $molform = <STDIN>;
        C6H9 and Output was:
        ------Please enter single letter elements in caps only------ Enter the molecular formula of the compound = C6H9 Forbidden chars The molecular weight of the above compound is =
Re^5: How to avoid an alphabet and integer next to it in a string?
by DrHyde (Prior) on Mar 25, 2014 at 11:26 UTC

    Or simpler, H\d+. \d* will match *zero* or more digits, \d+ matches *one* or more.

    update: oh, wait, ignore me. You need to knock out the H in (eg) NaOH, which doesn't have a digit after it.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1079323]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (9)
As of 2014-09-18 11:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (111 votes), past polls