Solved

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

Posted on 2011-09-29
3
346 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

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 …
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…
Six Sigma Control Plans

635 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