The main problem with your attempted solution is that .* is greedy. This
means that the colon it would find is the last one on the line. Greedy
means that .* wants to maximize the number of characters found. The old
solution to this is to replace .* with [^:]*, which matches as many
non-colons as possible, which obviously will not advance the match
beyond the first colon. The newer and more general solution is to use
the non-greed variant on .*, which is .*? (? modifies the * to make it
non-greedy).
Luinrandir Hernsen wrote:
> Example string
> $Data=":2-Name1:4-Name2:3-Name3:1-Name4:";
>
> I need to get the following info
> :4-Name2:
>
> But All I can search with is
> :4-anything:
> In other words I know it starts with :4-
> then it has a name in the middle
> and ends with the next :
>
> my guess is
> $Info = ($Data =~ /(:4-*:)/);
> $Info if the var assigned the value sought.
> $Data is the string I am searching
> :4- and the last : are know
> * are the unknown characters, if any
>
> here is the program
> #!/usr/bin/perl
> use strict ;
>
> my $Data=":2-Name1:4-Name2:3-Name3:1-Name4:";
> my $Info = ($Data =~ /(:4-*:)/);
> print ">$Info<";
>
>