Solved

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

Posted on 2011-09-29
3
330 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Merge files & delete row based on criteria using Perl. 1 104
Perl output collect 7 67
perl to convert excel to csv 3 258
create a gui in perl 3 98
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…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

828 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