Solved

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

Posted on 2011-09-29
3
323 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
ID: 36819140
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
ID: 36819167
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
ID: 36819177
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

777 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