Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

regular expression ?!

by Anonymous Monk
on May 05, 2009 at 12:39 UTC ( [id://761916]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, I have a question,Initially I thought as a simple one !!! I have a header of a file which is some thing like
SNP Coor Alleles BM19228A BM19228B BM19229A BM19229B + BM19231A
The BM* could be more that 100s.I need to prefix the BMs with a string which is XXXXX_.

Though I could slurp the first header line from the entire file and succeeded in changing the first BM19228A to XXXXX_BM19228A. I am stuch with progressing them with other BMs . Could any one please give suggestions or directions in doing it?
thanks a lot

Replies are listed 'Best First'.
Re: regular expression ?!
by moritz (Cardinal) on May 05, 2009 at 12:42 UTC
    What have you done so far? Show us the code, maybe it can be easily extended.
      #!/usr/local/bin/perl use strict; use Getopt::Long; use sql_engine; my $string = "XXX2EX_"; #my (@grepnam, @names); while(<>) { chomp; if ($_ =~/SNP*/i) { #print $_, "\n"; if ($_ =~s/BM*/$string/) { print $_, "\n"; } } }
        Sorry its me again. In may actual code it works as if ($_ =~s/BM*/XXXEX_BM/)
Re: regular expression ?!
by Utilitarian (Vicar) on May 05, 2009 at 12:54 UTC
    have you added the global modifier to the regex? eg.
    $firstline=~s/(\s)BM/$1XXXXX_BM/g
Re: regular expression ?!
by ccn (Vicar) on May 05, 2009 at 12:51 UTC
    perl -ple 's/\bBM/XXXXX_BM/g if $. == 1' file
Re: regular expression ?!
by AnomalousMonk (Archbishop) on May 05, 2009 at 12:55 UTC
    I, too, find it hard to comment on code (you say you can make one substitution successfully) that is not presented.

    However, one direction for further investigation might be the g regex modifier (see perlre and links therefrom).

Re: regular expression ?!
by vinoth.ree (Monsignor) on May 05, 2009 at 14:05 UTC
    #!/usr/local/bin/perl use strict; use Getopt::Long; use sql_engine; my $string = "XXX2EX_"; #my (@grepnam, @names); while(<>) { chomp; if ($_ =~/SNP*/i) { #print $_, "\n"; if ($_ =~s/BM*/$string/) { print $_, "\n"; } } }

    In your code this line if ($_ =~s/BM*/$string/) substitute BM* with the literal string XXX2EX_ having it in the variable $string, to prefix XXXXX change the second if condition as  if ($_ =~s/(BM*)/XXXXX_$1/)

    Vinoth,G
Re: regular expression ?!
by JavaFan (Canon) on May 05, 2009 at 13:37 UTC
    Based on the single line example, I'd write it as:
    s/\b(?=BM)/XXXXX_/g;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://761916]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-24 04:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found