如何使用regexp替换部分字符串?

时间:2021-01-10 16:49:50

HI,
I have a string that looks like

嗨,我有一个看起来像的字符串

/dir/dir1/filename.txt

I want to replace the "filename.txt" with some other name leaving the "/dir/dir1" intact so after the replace the string would look like

我想用一些其他名称替换“filename.txt”,使“/ dir / dir1”保持原样,所以在替换字符串之后会看起来像

/dir/dir1/newfilename.txt

how would I do that using RegExp in Perl considering that I don't know the value of "filename"

考虑到我不知道“filename”的值,我怎么能在Perl中使用RegExp呢?

Many Thanks

P.S : "filename.txt" and "newfilename.txt" have been used for the purpose of making things simple when asking the question the original filename will vary.

P.S:“filename.txt”和“newfilename.txt”用于在询问原始文件名将变化的问题时使事情变得简单。

4 个解决方案

#1


$filename =~ s%/[^/]*$%/newfilename.txt%;
  • s%...%...% - use % as the delimiter, so you can use / in the pattern
  • s%...%...% - 使用%作为分隔符,因此您可以在模式中使用/

  • / - match a slash
  • / - 匹配斜杠

  • [^/]* - and any string not containing a slash
  • [^ /] * - 以及任何不包含斜杠的字符串

  • $ - up to the end of the string
  • $ - 直到字符串的结尾

#2


I would suggest against using regular expressions to fiddle with filenames/paths. Directory Separators, valid/invalid characters vary from one platform to the next and can be a problem to hunt down. I advise you try File::Spec to extract the filename and then replace it with whatever you want

我建议不要使用正则表达式来摆弄文件名/路径。目录分隔符,有效/无效字符因平台而异,可能是一个需要解决的问题。我建议你尝试File :: Spec来提取文件名,然后用你想要的任何东西替换它

#3


Regular expressions is the wrong tool for your problem. Regexes are fantastic, work most of the time but are not always the cleanest (readability) or fastest solution.

正则表达式是您的问题的错误工具。正则表达式很棒,大部分时间都在工作,但并不总是最干净(可读性)或最快的解决方案。

I would propose to use the classic File::Basename. In contrast with File::Spec, File::Basename is a core module (nothing to install!) and really easy to use:

我建议使用经典的File :: Basename。与File :: Spec相比,File :: Basename是一个核心模块(无需安装!)并且非常易于使用:

    use File::Basename;

    my $basename = basename($fullname,@suffixlist);
    my $dirname  = dirname($fullname);

My favorite use:

我最喜欢的用途:

    my $filename = basename($path);

#4


Try something like this...

尝试这样的事......

#!/usr/bin/perl

use strict;

my $string = "/dir/dir1/filename.txt";
my @string = split /\//, $string;
$string[-1] = "newfilename.txt";
print join "/", @string;

#1


$filename =~ s%/[^/]*$%/newfilename.txt%;
  • s%...%...% - use % as the delimiter, so you can use / in the pattern
  • s%...%...% - 使用%作为分隔符,因此您可以在模式中使用/

  • / - match a slash
  • / - 匹配斜杠

  • [^/]* - and any string not containing a slash
  • [^ /] * - 以及任何不包含斜杠的字符串

  • $ - up to the end of the string
  • $ - 直到字符串的结尾

#2


I would suggest against using regular expressions to fiddle with filenames/paths. Directory Separators, valid/invalid characters vary from one platform to the next and can be a problem to hunt down. I advise you try File::Spec to extract the filename and then replace it with whatever you want

我建议不要使用正则表达式来摆弄文件名/路径。目录分隔符,有效/无效字符因平台而异,可能是一个需要解决的问题。我建议你尝试File :: Spec来提取文件名,然后用你想要的任何东西替换它

#3


Regular expressions is the wrong tool for your problem. Regexes are fantastic, work most of the time but are not always the cleanest (readability) or fastest solution.

正则表达式是您的问题的错误工具。正则表达式很棒,大部分时间都在工作,但并不总是最干净(可读性)或最快的解决方案。

I would propose to use the classic File::Basename. In contrast with File::Spec, File::Basename is a core module (nothing to install!) and really easy to use:

我建议使用经典的File :: Basename。与File :: Spec相比,File :: Basename是一个核心模块(无需安装!)并且非常易于使用:

    use File::Basename;

    my $basename = basename($fullname,@suffixlist);
    my $dirname  = dirname($fullname);

My favorite use:

我最喜欢的用途:

    my $filename = basename($path);

#4


Try something like this...

尝试这样的事......

#!/usr/bin/perl

use strict;

my $string = "/dir/dir1/filename.txt";
my @string = split /\//, $string;
$string[-1] = "newfilename.txt";
print join "/", @string;