Solved

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

Posted on 2011-09-29
3
307 Views
Last Modified: 2012-05-12
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,
0
Comment
Question by:Tolgar
  • 2
3 Comments
 
LVL 31

Expert Comment

by:farzanj
Comment Utility
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.
0
 

Author Comment

by:Tolgar
Comment Utility
is it possible to separate them into two lines to make it more readable?

Thanks,

0
 
LVL 31

Accepted Solution

by:
farzanj earned 500 total points
Comment Utility
Yes,

First is just the string that I assumed you have
my $str = "/mycmp/kmg/vel/kat/Atk/cache/0915/0914222222.submit.tolgar.111111";

Then you can apply regular expression on it.
$str =~ m{/kat/(\w+).*\.(\w+)};

Then you have results in $1 and $2 whatever you want to do with them.

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now