Link to home
Start Free TrialLog in
Avatar of Tolgar
Tolgar

asked on

How to extract a value from a string seperated by dot character in Perl?

Hi,
I have a variable somewhere in my code.

The string looks like this:

/mycmp/kmg/vel/kat/Atk/cache/0915/0914222222.submit.tolgar.111111

Open in new window


From this string I want to extract the followings:

Atk
111111

Open in new window


The grammar rule is:

The beginning of the string is always same:

/mycmp/kmg/vel/kat/

Open in new window


and the number that I want is always after the last dot character till the end


In general according to the rule I can write:

/mycmp/kmg/vel/kat/X/cache/ANYTHING.X

Open in new window


I want to extract X values from this string

How can I do it in Perl?

Thanks,
Avatar of farzanj
farzanj
Flag of Canada image

my $str = "/mycmp/kmg/vel/kat/Atk/cache/0915/0914222222.submit.tolgar.111111
";
my ($val1, $val2) = $str =~ m{/kat/(\w+).*\.(\w+)};

$val1 and $val2 is what you need.
Avatar of Tolgar
Tolgar

ASKER

is it possible to separate them into two lines to make it more readable?

Thanks,

ASKER CERTIFIED SOLUTION
Avatar of farzanj
farzanj
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial