开发者

Perl - How to change every $variable occurrence of ";" in a string

开发者 https://www.devze.com 2023-02-11 12:15 出处:网络
Very new here so be gentle. :) Here is the jist of what I want to do: I want to take a string that is made up of numbers separated by semi-colons (ex. 6;7;8;9;1;17;4;5;90) and replace every \"X\" nu

Very new here so be gentle. :)

Here is the jist of what I want to do:

I want to take a string that is made up of numbers separated by semi-colons (ex. 6;7;8;9;1;17;4;5;90) and replace every "X" number of semicolons with a "\n" instead. The "X" number will be defined by the user.

So if:

$string = "6;7;8;9;1;17;4;5;90";  
$Nth_number_of_semicolons_to_replace = 3;

The output should be:

6;7;8\n9;1;17\n4;5;90

I've found lots on changing the Nth occurrence of something but I haven't been able to find anything on changing every Nth occurrence of something like I am trying to descri开发者_如何学Cbe above.

Thanks for all your help!


use List::MoreUtils qw(natatime); 
my $input_string = "6;7;8;9;1;17;4;5;90"; 
my $it = natatime 3, split(";", $input_string);
my $output_string; 

while (my @vals = $it->()) { 
    $output_string .= join(";", @vals)."\n";
}


Here is a quick and dirty answer.

my $input_string = "6;7;8;9;1;17;4;5;90";
my $count = 0;
$input_string =~ s/;/++$count % 3 ? ";" : "\n"/eg;


Don't have time for a full answer now, but this should get you started.

$string = "6;7;8;9;1;17;4;5;90";  
$Nth_number_of_semicolons_to_replace = 3;
my $regexp = '(' . ('\d+;' x ($Nth_number_of_semicolons_to_replace - 1)) . '\d+);';
$string =~ s{ $regexp ) ; }{$1\n}xsmg


sub split_x{
  my($str,$num,$sep) = @_;
  return unless defined $str;

  $num ||= 1;
  $sep = ';' unless defined $sep;

  my @return;
  my @tmp = split $sep, $str;
  while( @tmp >= $num ){
    push @return, join $sep, splice @tmp, 0, $num;
  }
  push @return, join $sep, @tmp if @tmp;

  return @return;
}

print "$_\n" for split_x '6;7;8;9;1;17;4;5;90', 3

print join( ',', split_x( '6;7;8;9;1;17;4;5;90', 3 ) ), "\n";


my $string = "6;7;8;9;1;17;4;5;90";
my $Nth_number_of_semicolons_to_replace = 3;

my $num = $Nth_number_of_semicolons_to_replace - 1;
$string =~ s{ ( (?:[^;]+;){$num} [^;]+ ) ; }{$1\n}gx;

print $string;

prints:

6;7;8
9;1;17
4;5;90

The regex explained:

s{ 
  (                   # start of capture group 1
     (?:[^;]+;){$num} # any number of non ';' characters followed by a ';' 
                      # repeated $num times
     [^;]+            # any non ';' characters
  )                   # end of capture group
  ;                   # the ';' to replace
}{$1\n}gx;            # replace with capture group 1 followed by a new line


If you've got 5.10 or higher, this could do the trick:

#!/usr/bin/perl

use strict;
use warnings;

my $string = '1;2;3;4;5;6;7;8;9;0';
my $n = 3;

my $search = ';.*?' x ($n -1);

print "string before: [$string]\n";

$string =~ s/$search\K;/\n/g;

print "print string after: [$string]\n";

HTH, Paul

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号