Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Regular expression

by Archana (Initiate)
on Aug 27, 2007 at 08:58 UTC ( [id://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?

Replies are listed 'Best First'.
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 (Patriarch) 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
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-04-18 07:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found