in reply to place holder

Just when you think you can move on, you get into another problem. 2 questions: 1. To make things even more complicated s/-(bc)/-:$1/g looks for substrings as well as strings (bc or b or c), I need it to look only for the string (bc). 2. this is a different problem, I want to split a string with only the first blank (or series of blanks) as an argument. example: split "a b c" into "a" and "b c".

Replies are listed 'Best First'.
Re: Re: place holder
by Albannach (Monsignor) on Aug 29, 2001 at 06:36 UTC
    1. Many options, but basically you must account for the difference between substrings and stand-alone strings, and this appears to be whitespace in your example data, hence something like:
      s/(?<=\s)-([bc]\s)/-:$1/g;
    2. You can stop split from continuing using the 3rd argument:
      split( '\s', 'a b c', 2);
      though if there may be leading whitespace before the 'a' you'd better use the special case of splitting on a single space, which will discard any leading whitespace:
      split( ' ', '  a b c', 2);

    --
    I'd like to be able to assign to an luser

Re: Re: place holder
by ChemBoy (Priest) on Aug 29, 2001 at 09:21 UTC

    s/-([bc])/-:$1/g; actually doesn't look for the whole string at all--[bc] is a character class, not a literal string.

    This means that your regex translates to

    • Find a dash ("-")
    • Followed by either a "b" or a "c" (save this character in $1)

    when you wanted that second line to read "the string 'bc' (save this string in $1)".

    The easy solution is to take those brackets out:

    s/-(bc)/-:$1/g;

    and for what I'm guessing is the next step (multiple strings that could match), you can use

    s/-(foo|bar|bc|whatever)/-:$1/g;

    But in fact you might be better off with tachyon's suggestion:

    s/(?<=-)(?=foo|bar|baz)/:/g; #assuming you have 5.005 or better

    The longer but more rewarding solution is to take another couple of trips through perlre when you have a moment--regexen are not the fastest thing in the world to pick up (for us mere mortals, anyway) but they reward study very nicely. :-)

    Your other question just needs the last argument to split:

    ($first, $second) = split ' ', "a b c", 2;
    and you're all set. Good luck!

    Update: fixed boneheaded mistake in split (thanks, Hofmator!)



    If God had meant us to fly, he would *never* have given us the railroads.
        --Michael Flanders