Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Regular expression

by Archana (Initiate)
on Aug 27, 2007 at 08:58 UTC ( #635250=perlquestion: print w/ replies, xml ) Need Help??
Archana has asked for the wisdom of the Perl Monks concerning the following question:

I have string like this for eg:- "Transcription factor" promoter DNA what i want is "Transcription factor",promoter,DNA. When i try to substitute for space in between the phrases("transcription factor") comma is substituted. how do i solve this problem?

Comment on Regular expression
Re: Regular expression
by Samy_rio (Vicar) on Aug 27, 2007 at 09:33 UTC

    Hi Archana, try like this,

    use strict; use warnings; my $str ='"Transcription factor" promoter DNA "Transcription factor" p +romoter DNA.'; my @str = split//, $str; my $fla = 1; for (@str){ if (($_ eq "\"") && $fla) { $fla = 0; }elsif (($_ eq "\"") && !($fla)){ $fla = 1; } $_ =~ s/ /\,/ if ($fla); } print join'',@str; __END__ Output: ------- "Transcription factor",promoter,DNA,"Transcription factor",promoter,DN +A.

    TIMTOWTDI, we can achieve the expected output using RegExp as of below:

    $str =~ s/((?:(?!\").)*)(\"[^\"]+\")((?:(?!\").)*)/ my $fi = $1;my $se = $2; my $th = $3; $fi =~ s| |\,|g;$th =~ s| |\,|g; "$fi$se$th"/egs;

    Regards,
    Velusamy R.


    eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

Re: Regular expression
by ikegami (Pope) on Aug 27, 2007 at 15:48 UTC

    A tokenizer is what you want. Here's an implementation:

    my $line ='"Transcription factor" promoter DNA'; my $fixed; for ($line) { /\G ( \" [^"]+ \" ) /xgc && do { $fixed .= $1; redo }; /\G ( [^"\s]+ ) /xgc && do { $fixed .= $1; redo }; /\G \s+ /xgc && do { $fixed .= ','; redo }; last; } print("$fixed\n");

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (11)
As of 2014-09-18 21:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (125 votes), past polls