Perl serach string that starts with a pattern

Posted on 2014-08-19
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

    You need to backslash the asterisks in your search:

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

    Open in new window


    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

    In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
    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 (…
    Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
    The viewer will learn how to dynamically set the form action using jQuery.

    733 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

    23 Experts available now in Live!

    Get 1:1 Help Now