| Author |
simple string extracting
|
|
| Sharif Islam 2006-10-30, 7:09 pm |
| I have some strings that look like this:
info:sid/SOMECHAR:abc
The part of the string I am interested in is 'SOMECHAR' (the 'info:sid/'
will always be there). Is there a better way to extract this?
#! /usr/bin/perl
use strict;
my $out;
my $string = "info:sid/SOMECHAR:xyz";
if($string =~ m/:(.*):/) {
($out = $1) =~ s/sid\///;
print $out ;
}
# perl string.pl
SOMECHAR
| |
| Xicheng Jia 2006-10-30, 7:09 pm |
| Sharif Islam wrote:
> I have some strings that look like this:
> info:sid/SOMECHAR:abc
>
> The part of the string I am interested in is 'SOMECHAR' (the 'info:sid/'
> will always be there). Is there a better way to extract this?
>
> #! /usr/bin/perl
> use strict;
>
> my $out;
> my $string = "info:sid/SOMECHAR:xyz";
How about :
$out = (split '[/:]', $string)[2];
Xicheng
> if($string =~ m/:(.*):/) {
> ($out = $1) =~ s/sid\///;
> print $out ;
> }
| |
| Paul Lalli 2006-10-30, 7:09 pm |
| Sharif Islam wrote:
> I have some strings that look like this:
> info:sid/SOMECHAR:abc
>
> The part of the string I am interested in is 'SOMECHAR' (the 'info:sid/'
> will always be there). Is there a better way to extract this?
>
> #! /usr/bin/perl
> use strict;
>
> my $out;
> my $string = "info:sid/SOMECHAR:xyz";
> if($string =~ m/:(.*):/) {
> ($out = $1) =~ s/sid\///;
> print $out ;
> }
Why are you doing two regular expressions?
if ($string =~ m{^info:sid/(.*):}) {
print $1;
}
Paul Lalli
| |
| yankeeinexile@gmail.com 2006-10-30, 7:09 pm |
| Sharif Islam <mislam@spam.uiuc.edu> writes:
> I have some strings that look like this:
> info:sid/SOMECHAR:abc
>
> The part of the string I am interested in is 'SOMECHAR' (the
> 'info:sid/' will always be there). Is there a better way to extract
> this?
>
> #! /usr/bin/perl
> use strict;
>
> my $out;
> my $string = "info:sid/SOMECHAR:xyz";
> if($string =~ m/:(.*):/) {
> ($out = $1) =~ s/sid\///;
> print $out ;
> }
Why use two regexps when one is just as good?
#!/usr/bin/perl
use strict;
use warnings;
my $out;
my $string = "info:sid/SOMECHAR:xyz";
if(my ($out) = $string =~ m|info:sid/(.*):xyz| ) {
print "$out\n";
}
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Lawrence Statton - lawrenabae@abaluon.abaom s/aba/c/g
Computer software consists of only two components: ones and
zeros, in roughly equal proportions. All that is required is to
sort them into the correct order.
| |
| Mirco Wahab 2006-10-30, 7:09 pm |
| Thus spoke Sharif Islam (on 2006-10-25 18:29):
> I have some strings that look like this:
> info:sid/SOMECHAR:abc
> my $string = "info:sid/SOMECHAR:xyz";
...
print +($string =~ /sid\/(.+?):/);
...
or
...
($out) = ($string =~ /sid\/(.+?):/);
...
Regards
M.
| |
| Sharif Islam 2006-10-30, 7:09 pm |
| Paul Lalli wrote:
> Sharif Islam wrote:
>
>
>
> Why are you doing two regular expressions?
>
> if ($string =~ m{^info:sid/(.*):}) {
> print $1;
> }
>
thanks. that is better.
--sharif
|
|
|
|