[Last Call] Learn how to a build a cloud-first strategyRegister Now


Perl serach string that starts with a pattern

Posted on 2014-08-19
Medium Priority
Last Modified: 2014-08-20
Example: MAIN1*PA*1231*ASDFSDF*ARWTFGADF****BDFBV*140056546

I want to extract ID from the above string which is nothing but the string after 9th '*' and the line should always start with "MAIN1*PA*1231" so the ID in the above case is '140056546'

I asked a similar question couple of days back - so from that answer I adjusted my perl script as below but it has compilation errors which i am not able to fix...
so can some one tell me what's wrong with my script...


use 5.006;
use strict;
use warnings;

  my $str = 'KAYAK*21312*123*12312*123$BROAD*P*18*SDFGASDF******ASDF$MAIN1*PA*1231*ASDFSDF*ARWTFGADF****BDFBV*140056546$ASDF*ASDF8*112157409*ASDF';
  my @lines = split /\$/, $str;
  my $mID;
  my @emps = ();
  for my $i (2..@lines-1) {
  if ($lines[$i] =~ /^MAIN1*PA*1231\*/) {
    $mID = (split /\*/, $lines[$i])[9];
    push(@emps, $mID);
  foreach(@emps) {
    print "$_";
Question by:shragi
1 Comment
LVL 23

Accepted Solution

nemws1 earned 2000 total points
ID: 40271653
You need to backslash the asterisks in your search:

if ($lines[$i] =~ /^MAIN1\*PA\*1231\*/) {

Open in new window


Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses
Course of the Month18 days, 8 hours left to enroll

825 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