查找Perl中的空白索引

时间:2017-06-02 22:45:12

标签: perl substring

我试图在Perl中找到字符串中的空格索引。

例如,如果我有字符串

stuff/more stuffhere

我想选择"更多"用子串方法。我可以找到" /"的索引但是没有想出如何找到白色空间的索引。尝试选择的子字符串I的长度会有所不同,因此我无法对索引进行硬编码。字符串中只有一个空格(除了字符串结尾之后的空格)。

另外,如果有人对如何做到这一点有任何更好的想法,我很感激听到他们。我对编程很新,所以我愿意接受建议。感谢。

4 个答案:

答案 0 :(得分:2)

只需使用index

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

my $string = 'stuff/more stuffhere';
my $index_of_slash = index $string, '/';
my $index_of_space = index $string, ' ';

say "Between $index_of_slash and $index_of_space.";

输出

Between 5 and 10.

哪个是对的:

0         1
01234567890123456789
stuff/more stuffhere

如果通过“空白”也表示标签或其他内容,则可以使用与pos匹配的正则表达式:

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

my $string = "stuff/more\tstuffhere";

$string =~ m{(/.*)\s}g;
my $index_of_space = pos($string) - 1;
my $index_of_slash = $index_of_space - length $1;
say "Between $index_of_slash and $index_of_space.";

答案 1 :(得分:1)

如您所述,您希望在第一个/之间选择一词 和它后面的第一个空间。 如果是这种情况,您可能不需要任何索引(您只需要 这个词)。

在文本中查找内容的完美工具是 regex 。 请查看以下代码:

$txt = 'stuff/more stuffxx here';
if ($txt =~ /\/(.+?) /) {
  print "Match: $1.\n";
}

使用的正则表达式尝试匹配:

  • 斜线,
  • 任何字符的非空序列(注意? - 不情愿 版本),包含在捕获组中,
  • 一个空间。

匹配后$1包含第一个捕获的内容 捕获组,即"你的"字。

但如果出于任何原因你有兴趣开始和结束 抵消这个词,你可以从$-[1]阅读它们 和$+[1](第一个捕获组的开始/结束索引)。

答案 2 :(得分:0)

数组@-@LAST_MATCH_START)和@+@LAST_MATCH_END)给出了上次成功子匹配的开始和结束的偏移量。请参阅Regex related variables in perlvar

您可以捕获真实目标,然后使用$+[0] 之后读取右侧的偏移量

  

<强> @ +
  此数组保存当前活动动态范围中最后成功子匹配的末尾的偏移量。 $+[0]是整个匹配结束字符串的偏移量。这与pos函数在匹配的变量上调用时返回的值相同。

实施例

my $str = 'target and target with spaces';

while ($str =~ /(target)\s/g)
{
    say "Position after match: $+[0]"
} 

打印

Position after match: 7
Position after match: 18

这些是'target'之后的位置,也就是它之后的空格。

或者您可以代替\s并使用$-[1] + 1(匹配的第一个位置,即空格)。

答案 3 :(得分:0)

您可以使用

my $str = "stuff/more stuffhere";
if ($str =~ m{/\K\S+}) {
   ... substr($str, $-[0], $+[0] - $-[0]) ...
}

但为什么substr?那里很奇怪。也许如果你告诉我们你真正想做什么,我们可以提供更好的选择。以下是三种情况:

数据提取:

my $str = "stuff/more stuffhere";
if ( my ($word) = $str =~ m{/(\S+)} ) {
   say $word;  # more
}

数据替换:

my $str = "stuff/more stuffhere";
$str =~ s{/\K\S+}{REPLACED};
say $str;  # stuff/REPLACED stuffhere

数据替换(动态):

my $str = "stuff/more stuffhere";
$str =~ s{/\K(\S+)}{ uc($1) }e;
say $str;  # stuff/MORE stuffhere