Re: how to find particular string and store in to variable
by stevieb (Canon) on Jun 25, 2015 at 12:56 UTC
|
What have you tried? Are all of your lines going to be exactly the same with an integer at the end of the line?
#!/usr/bin/perl
use warnings;
use strict;
my $str = "http://172.20.37.115:8080/se/1.0/provision/subscribers/1989
+68";
$str =~ /.*\/(\d+)$/;
my $var = $1 || '';
print "$var\n";
-stevieb | [reply] [d/l] |
|
|
#!/usr/bin/perl
use warnings;
use strict;
my $str = "http://172.20.37.115:8080/se/1.0/provision/subscribers/1989
+68";
$str =~ /.*\/(\d+)$/;
my $var = $1 || '';
print "$var\n";
# prints 198968
my $str2 = http://172.20.37.115:8080/se/1.0/provision/subscribers/foo"
+;
$str2 =~ /.*\/(\d+)$/;
my $var2 = $1 || '';
print "$var2\n";
#prints 198968
May be better to do:
use warnings;
use strict;
my $str = "http://172.20.37.115:8080/se/1.0/provision/subscribers/1989
+68";
if( $str =~ /.*\/(\d+)$/ ) {
my $var = $1 || '';
print "$var\n";
}
| [reply] [d/l] [select] |
|
|
But I was matching against (\d*) \z which cannot fail, although it could match an empty string of digits — which can be useful in and of itself!
Update: Interestingly, what is captured | assigned to $var in a statement like
my ($var) = $s =~ m{ (\d+) \z }xms;
is not the same as what is captured to | not necessarily the same as the value of $1:
c:\@Work\Perl\monks>perl -wMstrict -le
"'zot' =~ m{ (zot) }xms;
;;
for my $s (
'http://172.20.37.115:8080/se/1.0/pro/subs/198968/',
'http://172.20.37.115.8080/se/1.0/pro/123456',
) {
my ($var) = $s =~ m{ (\d+) \z }xms;
print '$var is ', defined($var) ? '' : 'UN', 'defined';
print qq{'$s' -> '$var' (\$1 is '$1')};
}
"
$var is UNdefined
Use of uninitialized value $var in concatenation (.) or string at -e l
+ine 1.
'http://172.20.37.115:8080/se/1.0/pro/subs/198968/' -> '' ($1 is 'zot'
+)
$var is defined
'http://172.20.37.115.8080/se/1.0/pro/123456' -> '123456' ($1 is '1234
+56')
This warrants a bit more research!
Give a man a fish: <%-(-(-(-<
| [reply] [d/l] [select] |
|
|
|
|
|
|
|
|
|
|
|
Assuming, as you do, the extraction of a group of digits at the absolute end of a string, the .*\/ portion of the regex is unnecessary (although it does no harm), as is the subsequent
my $var = $1 || '';
capture/fixup statement. (Update: But see also the reply of 1nickt below.) An alternative would be:
c:\@Work\Perl\monks>perl -wMstrict -le
"for my $s (
'http://172.20.37.115:8080/se/1.0/pro/subs/198968',
'http://172.20.37.115.8080/se/1.0/pro/123456/',
) {
my ($var) = $s =~ m{ (\d*) \z }xms;
print qq{'$s' -> '$var'};
}
"
'http://172.20.37.115:8080/se/1.0/pro/subs/198968' -> '198968'
'http://172.20.37.115.8080/se/1.0/pro/123456/' -> ''
with the regex expression m{ \d* \z }xmsg (note the added /g modifier) also working.
Give a man a fish: <%-(-(-(-<
| [reply] [d/l] [select] |
Re: how to find particular string and store in to variable
by karlgoethebier (Abbot) on Jun 25, 2015 at 17:26 UTC
|
#!/usr/bin/env perl
use strict;
use warnings;
use URI::URL;
use feature qw(say);
my $url = URI::URL->new(
q(http://172.20.37.115:8080/se/1.0/provision/subscribers/198968));
# say( map { ( split /\// )[-1] } $url->path );
my ($var) = map { ( split /\// )[-1] } $url->path;
say $var;
__END__
karls-mac-mini:monks karl$ ./1131943.pl
198968
Please see also URI::URL, map, split and TMTOWTDI.
Update: For a better solution (my @paths = $url->path_segments;) please see below.
Regards, Karl
«The Crux of the Biscuit is the Apostrophe»
| [reply] [d/l] [select] |
|
|
use URI::URL;
my $url = URI::URL->new(
'http://172.20.37.115:8080/se/1.0/provision/subscribers/198968');
my @paths = $url->path_segments;
my $last = $paths[-1];
print "$last\n";
__END__
198968
| [reply] [d/l] |
|
|
| [reply] |
Re: how to find particular string and store in to variable
by marto (Cardinal) on Jun 26, 2015 at 09:50 UTC
|
This is a fairly simple problem, a reasonably common task regardless of programming language. Of course there is always more than one way to do it. Please note, the following is not a criticism but an observation, since you had to ask how to achieve this the suggestion a regex may be beyond your abilities at the moment. See xkcd://1171, I'll leave the background reading to you.
In the interest of keeping things very simple and presenting alternatives, below is an example using only perl built-ins, so not relying on an external module or regular expressions:
#!/usr/bin/perl
use strict;
use warnings;
my $url = "http://172.20.37.115:8080/se/1.0/provision/subscribers/1989
+68";
# use rindex to get the position of the rightmost / character, add 1
# use substr to extract the remainder of the string after this positio
+n
# perldoc -f rindex
# perldoc -f substr
my $subscriber = substr( $url, ( rindex($url, '/') + 1 ) );
print "$subscriber";
Some links which may be of interest:
| [reply] [d/l] |
Re: how to find particular string and store in to variable
by 1nickt (Canon) on Jun 25, 2015 at 13:22 UTC
|
my $string = 'http://172.20.37.115:8080/se/1.0/provision/subscribers/1
+98968';
my ($wanted) = $string =~ m/ \d+$ /gx;
| [reply] [d/l] |
|
|
| [reply] [d/l] [select] |
Re: how to find particular string and store in to variable
by marinersk (Priest) on Jun 26, 2015 at 05:53 UTC
|
karlgoethebier and Anonymous Monk shows what is probably the best answer (using URI::URLand pop). The regular expression grabbing digits at the end is also reasonable, but with a fair number of caveats, many of which are on this thread.
My first instict (assuming the URL is generally consistent) would be to splitthe URL on its slashes and popthe last entry off the result. To be fair, that would be because I wasn't familiar with URI::URLuntil I read about it on this thread. :-)
Good luck; and please, next time, show us what you've tried on the original post, okay?
| [reply] [d/l] [select] |
A reply falls below the community's threshold of quality. You may see it by logging in. |