如何在perl中分割2个部分的字符串

时间:2022-09-13 14:22:37

so I need to divide my string in perl for 2 parts. For example I have:

所以我需要在perl中将我的字符串分成两部分。例如,我有:

$string = "../dira/dirb/*.txt"

And I want to divide it on:

我想把它分开:

$stringA = "../dira/dirb"
$stringB = "*.txt"

But if I have:

但如果我有:

$string = "dira/dirb/dirc/.../dirn/test.pl";

I want to divie it on:

我想把它分开:

$stringA = "dira/dirb/dirc/.../dirn"
$stringB = "test.pl"

Somebody have idea how can I do it? I tried to do something like:

有人知道我该怎么办?我尝试过这样的事情:

$howmany++ while $string =~ m/\//g;

So I know how many slashes I have. But I have no idea what I can do more with this :/

所以我知道我有多少斜杠。但是我不知道我能用这个做些什么:/

3 个解决方案

#1


you can try something like this:

你可以尝试这样的事情:

$string =~ m|^(.*)/(.*)$|;
($stringA,$stringB) = ($1,$2);
print "stringA = $stringA\n";
print "stringB = $stringB\n";

= edit: =

=编辑:=

restrict to certain values of stringB:

限制为stringB的某些值:

if($string =~ m|^(.*)/(.*\.pl)$|) {
    ($stringA,$stringB) = ($1,$2);
    print "stringA = $stringA\n";
    print "stringB = $stringB\n";
}

#2


Use Path::Tiny:

#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };

use Path::Tiny;

for my $path (qw( ../dira/dirb/*.txt
                  dira/dirb/dirc/.../dirn/test.pl
             )) {
    my $path_o = 'Path::Tiny'->new($path);
    my $basename = $path_o->basename;
    my $dirname = $path_o->dirname;
    $dirname =~ s=/$==;               # Remove the trailing slash.
    say $basename, ' ', $dirname;
}

#3


You can use File::Basename functions to parse file paths:

您可以使用File :: Basename函数来解析文件路径:

#!/usr/bin/env perl
use strict;
use warnings;
use File::Basename;

my $string = "../dira/dirb/*.txt";
my $stringA = dirname($string);
my $stringB = basename($string);

printf "String A: %-25sString B: %s\n", $stringA, $stringB;

$string = "dira/dirb/dirc/.../dirn/test.pl";
$stringA = dirname($string);
$stringB = basename($string);

printf "String A: %-25sString B: %s\n", $stringA, $stringB;

#1


you can try something like this:

你可以尝试这样的事情:

$string =~ m|^(.*)/(.*)$|;
($stringA,$stringB) = ($1,$2);
print "stringA = $stringA\n";
print "stringB = $stringB\n";

= edit: =

=编辑:=

restrict to certain values of stringB:

限制为stringB的某些值:

if($string =~ m|^(.*)/(.*\.pl)$|) {
    ($stringA,$stringB) = ($1,$2);
    print "stringA = $stringA\n";
    print "stringB = $stringB\n";
}

#2


Use Path::Tiny:

#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };

use Path::Tiny;

for my $path (qw( ../dira/dirb/*.txt
                  dira/dirb/dirc/.../dirn/test.pl
             )) {
    my $path_o = 'Path::Tiny'->new($path);
    my $basename = $path_o->basename;
    my $dirname = $path_o->dirname;
    $dirname =~ s=/$==;               # Remove the trailing slash.
    say $basename, ' ', $dirname;
}

#3


You can use File::Basename functions to parse file paths:

您可以使用File :: Basename函数来解析文件路径:

#!/usr/bin/env perl
use strict;
use warnings;
use File::Basename;

my $string = "../dira/dirb/*.txt";
my $stringA = dirname($string);
my $stringB = basename($string);

printf "String A: %-25sString B: %s\n", $stringA, $stringB;

$string = "dira/dirb/dirc/.../dirn/test.pl";
$stringA = dirname($string);
$stringB = basename($string);

printf "String A: %-25sString B: %s\n", $stringA, $stringB;