在 Perl 中查找大字符串中找到子字符串的行号

发布于 2024-10-16 07:26:01 字数 366 浏览 10 评论 0原文

在 perl 字符串中搜索子字符串的行号的最佳方法是什么?例如: 在中搜索“escape”

"How to Format
► put returns between paragraphs
► for linebreak add 2 spaces at end
► _italic_ or **bold**
► indent code by 4 spaces
► backtick escapes `like _so_`
► quote by placing > at start of line
► to make links
<http://foo.com>
[foo](http://foo.com)"

应该给出 6 作为行号。

What is the best way to search for the line number for a substring in perl string? For e.x.:
Searching for "escape" in

"How to Format
► put returns between paragraphs
► for linebreak add 2 spaces at end
► _italic_ or **bold**
► indent code by 4 spaces
► backtick escapes `like _so_`
► quote by placing > at start of line
► to make links
<http://foo.com>
[foo](http://foo.com)"

should give 6 as the line number.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

初见 2024-10-23 07:26:02

我将通过迭代搜索字符串中的目标模式和换行符来解决这个问题:

my $str = "<your string above>";
my $linenum = 1;
my $found = 0;

while ($str =~ /(escape)|\n/g) {
    $found = 1, last if defined $1;
    ++$linenum;
}

pos($str) = 0;  # Reset last match position

if ($found) {
    print "Found match on line $linenum\n";
} else {
    print "No match found\n";
}

I would approach this problem by iteratively searching the string for both the target pattern and for newline characters:

my $str = "<your string above>";
my $linenum = 1;
my $found = 0;

while ($str =~ /(escape)|\n/g) {
    $found = 1, last if defined $1;
    ++$linenum;
}

pos($str) = 0;  # Reset last match position

if ($found) {
    print "Found match on line $linenum\n";
} else {
    print "No match found\n";
}
绝不服输 2024-10-23 07:26:02

要在 Perl/CGI 接口中打印文件中的特定行:

open(FILE,"<report_timing.log");
my @list = grep /\bslack\b/, <FILE>;
chomp @list;
print "$_\n<P>" foreach @list;

To print a specific line from a file in Perl/CGI interface:

open(FILE,"<report_timing.log");
my @list = grep /\bslack\b/, <FILE>;
chomp @list;
print "$_\n<P>" foreach @list;
死开点丶别碍眼 2024-10-23 07:26:01

想到了另一个解决办法。在最近的 Perls 中,您可以在字符串上打开文件句柄,然后只需使用特殊的 $. 变量来自动跟踪行号:

open my $handle, '<', \$str;
my $linenum;

while (<$handle>) {
    $linenum = $., last if /escape/;
}

close $handle;

if (defined $linenum) {
    print "Found match on line $linenum\n";
} else {
    print "No match found\n";
}

Thought of another solution. In sufficiently recent Perls, you can open a filehandle onto a string, and then just use the special $. variable to automatically keep track of the line number:

open my $handle, '<', \$str;
my $linenum;

while (<$handle>) {
    $linenum = $., last if /escape/;
}

close $handle;

if (defined $linenum) {
    print "Found match on line $linenum\n";
} else {
    print "No match found\n";
}
纵性 2024-10-23 07:26:01

我将这样做:

my $string = 'How to Format
- put returns between paragraphs
- for linebreak add 2 spaces at end
- _italic_ or **bold**
- indent code by 4 spaces
- backtick escapes `like _so_`
- quote by placing > at start of line
- to make links
<http://foo.com>
[foo](http://foo.com)';

if ($string =~ /escape/) {
  # Count the number of newlines before the match.
  # Add 1 to make the first line 1 instead of 0.
  my $line = 1 + substr($string, 0, $-[0]) =~ tr/\n//;

  print "found at line $line\n";
}

这可以避免执行任何行计数工作,除非确实找到了字符串。它使用 @- 变量来找出匹配开始,然后使用 tr< /a> 计算换行符。

Here's how I would do it:

my $string = 'How to Format
- put returns between paragraphs
- for linebreak add 2 spaces at end
- _italic_ or **bold**
- indent code by 4 spaces
- backtick escapes `like _so_`
- quote by placing > at start of line
- to make links
<http://foo.com>
[foo](http://foo.com)';

if ($string =~ /escape/) {
  # Count the number of newlines before the match.
  # Add 1 to make the first line 1 instead of 0.
  my $line = 1 + substr($string, 0, $-[0]) =~ tr/\n//;

  print "found at line $line\n";
}

This avoids doing any work counting lines unless the string is actually found. It uses the @- variable to find out where the match started, and then uses tr to count newlines.

埋葬我深情 2024-10-23 07:26:01

$. 这个特殊变量将为您提供输入文件行号,有关更多详细信息,请访问 http://perldoc.perl.org/perlvar.html

use strict;
use IO::Handle;
my $search = 'escapes';
while (my $line = <DATA>) {
       if ($line =~ /$search/){
            my $line_number = DATA->input_line_number();
        }
}

__DATA__
"How to Format
? put returns between paragraphs
? for linebreak add 2 spaces at end
? _italic_ or **bold**
? indent code by 4 spaces
? backtick escapes `like _so_`
? quote by placing > at start of line
? to make links
<http://foo.com>
[foo](http://foo.com)"

$. This special variable will give you the input file line number, for more detail go to http://perldoc.perl.org/perlvar.html

use strict;
use IO::Handle;
my $search = 'escapes';
while (my $line = <DATA>) {
       if ($line =~ /$search/){
            my $line_number = DATA->input_line_number();
        }
}

__DATA__
"How to Format
? put returns between paragraphs
? for linebreak add 2 spaces at end
? _italic_ or **bold**
? indent code by 4 spaces
? backtick escapes `like _so_`
? quote by placing > at start of line
? to make links
<http://foo.com>
[foo](http://foo.com)"
要走就滚别墨迹 2024-10-23 07:26:01

假设您的输入文本包含在单个标量中:

my $i = 0;
my $lineno;
for my $line (split(/\n/, $large_block_of_text)) {
 if($line =~ /escape/) {
  $lineno = $i;
  last;
 }
 $i++;
}

if(defined($lineno)) {
 print("'escape' is on line $lineno\n");
} else {
 print("'escape' was not found\n");
}

This is assuming that your input text is contained in a single scalar:

my $i = 0;
my $lineno;
for my $line (split(/\n/, $large_block_of_text)) {
 if($line =~ /escape/) {
  $lineno = $i;
  last;
 }
 $i++;
}

if(defined($lineno)) {
 print("'escape' is on line $lineno\n");
} else {
 print("'escape' was not found\n");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文