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

Hello, I'm really new in the perl-world and asking for some help. I'm using a barcode scanner and inside the scanner is running a perl programm which processes the scanned barcode. I can only set one regex to adjust the scanned code. The result is send to the RS232-port. The host which is connected to the scanner needs a string with a fixed length. SO I want to change the scanned string with a regex to a new string with a fix length (= 13) by adding some extra ZERO's example: barcode: 1234567890123 -> 1234567890123 (-> do nothing) barcode: 123456789 -> 1234567890000 (-> adding 4 zero's) option: barcode: 123456789012345 -> maximum the first 13 characters. Thanks to all who's willing to help me. grtz, Bart.
  • Comment on How to convert a string with regex to a string with a fixed length?

Replies are listed 'Best First'.
Re: How to convert a string with regex to a string with a fixed length?
by lodin (Hermit) on Jul 29, 2008 at 16:17 UTC

    I don't quite understand the question. A regexp cannot change the string, it can only extract portions of the string.

    If you mean an substutation and you can evaluate the substitution part (but why would you then be limited to evaluate inside a s///?), you can use the following simple technique.

    s/(.*)/substr $1 . '0' x 13, 0, 13/se

    You just create a new string with all the zeros you could possibly need at the end, and then take the chars from the beginning.

    my @codes = ( '12345678', '1234567890123', '1234567890123456', ); my $len = 13; for my $var (@codes) { my $fixed = substr $var . '0' x $len, 0, $len; print "$fixed\n"; } __END__ 1234567800000 1234567890123 1234567890123

    lodin

Re: How to convert a string with regex to a string with a fixed length?
by moritz (Cardinal) on Jul 29, 2008 at 16:18 UTC
    If you can use an arbitrary substitution expression with your regex, try this:
    s/\b(\d+)\b/sprintf "%013d", $1/eg;

    If not, please tell us exactly what you can use - a single regular expression only matches stuff, but can't change the source string.

    Update: I missed that the 0's should actually be appended at the end. You can try to use lodin's solution inside the substitution part, though. Or to rescue my original solution:

    s/\b(\d+)\b/reverse sprintf "%013d", scalar reverse $1/eg;
      this is not working by me.
        This is not very helpful.

        Would you care to elaborate? In what way does it not work? Wrong output? if yes, which? Or does it raise an error? if so, which?

Re: How to convert a string with regex to a string with a fixed length?
by olus (Curate) on Jul 29, 2008 at 16:37 UTC

    Also with sprintf

    $barcode = sprintf("%.13s%s", $barcode, "0"x(13-length($barcode)));

    update thanks lodin, I misunderstood the question and assumed the input was always 13 or less characters. Fixed now

      As this is just a cumbersome way to do concatenation it does not fullfil the requirement to make $barcode shorter if it's over 13 chars long.

      lodin

Re: How to convert a string with regex to a string with a fixed length?
by MidLifeXis (Monsignor) on Jul 29, 2008 at 17:06 UTC

    sprintf("<%.13s>",  "123" . "0" x 13)

    --MidLifeXis

Re: How to convert a string with regex to a string with a fixed length?
by psini (Deacon) on Jul 29, 2008 at 18:16 UTC

    Simplifying a little the idea from moritz you could try this:

    /^(.{1,7})$/${1}000000/ /^(.{1,10})$/${1}000/ /^(.{1,11})$/${1}00/ /^(.{1,12})$/${1}0/

    Note that the single space between each pair of regex is important, as stated in the doc you posted.

    Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."