English 中文(简体)
如何用固定的表达方式限制扼杀
原标题:how to truncate a string using regular expression in perl
  • 时间:2010-08-03 19:25:19
  •  标签:
  • regex
  • perl

I have the following string in a file and want to truncate the string to no more than 6 char. how to do that using regular expression in perl?
the original file is:

编目短.。 :

<value>1234@google.com</value>
<value>1235@google.com</value>

I want to get file as:
cat shortstring.out

<value>1234@g</value>
<value>1235@g</value>

I have a code as follows, is there any more efficient way than using
s/<value>(wwwwww)(.*)/$1/;?

这是我守则的一部分:

    while (<$input_handle>) {                        # take one input line at a time
            chomp;
            if (/(d+@google.com)/) {
                    s/(<value>wwwwww)(.*)</value>/$1/;
                    print $output_handle "$_
";
              } else {
              print $output_handle "$_
";
            }
    }
问题回答

而是使用(管制局不是Perl的唯一特征,它为:-)

$str = substr($str, 0, 6);

rel=“noretinger”>http://perldoc.perl.org/Functions/substr.html

$ perl -pe  s/(<value>[^<]{1,6})[^<]*/$1/  shortstring.in
<value>1234@g</value>
<value>1235@g</value>

在您的提问中,使用

while (<$input_handle>) {
  s!(<value>)(.*?)(</value>)!$1 . substr($2,0,6) . $3!e
    if /(d+@google.com)/;
  print $output_handle $_;
}

或采用单一模式

while (<$input_handle>) {
   s!(<value>)(d+@google.com)(</value>)!$1 . substr($2,0,6) . $3!e;
  print $output_handle $_;
}

利用香料作为替代经营者的划界器,可防止http://en.wikipedia.org/wiki/Leaning_toothpick_syndrome” rel=“nofollow noretinger”>Leaning Toothpick Hopkins in </ Value>

<>光> 通常的 适用“分离”XML的定期表述。

解散方案:

#! /usr/bin/perl

use warnings;
use strict;

my $input_handle = *DATA;
open my $output_handle, ">&=", *STDOUT or die "$0: open: $!";

while (<$input_handle>) {
   s!(<value>)(d+@google.com)(</value>)!$1 . substr($2,0,6) . $3!e;
  print $output_handle $_;
}

__DATA__
<value>1234@google.com</value>
<value>1235@google.com</value>
<value>12@google.com</value>

产出:

$ ./prog.pl 
<value>1234@g</value>
<value>1235@g</value>
<value>12@goo</value>

为此:

s|(?<=<value>)(.*?)(?=</value>)|substr $1,0,6|e;

想像你那样,在标本中删除可能少于6个特性的案文,在这种情况下:

s/(<value>[^<]{1,6})[^<]*/$1/
s/<value>(.{1,6}).*/<value>$1</value>/;




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签