Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
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?

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 (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?
[Discipulus]: i'v installed ubuntu 16.04lts and i'm quite disappointed: is deadly simmilar to new win osses where you cannot find even simple things...
[Corion]: Hi Discipulus ;)
[Corion]: Mj1234: Maybe Ubuntu didn't package Devel::GDB? In that case, consider using local::lib and install the module from CPAN
[Corion]: But a quick Google search turns up this page, which seems to suggest that it should be packaged
[Discipulus]: iirc active perl has repositories only for last 2 o 3 major release of perl Mj1234. I strongly suggest portable version of strawberry perl and use cpan client to install modules
[choroba]: Also, who needs GDB when there's hdb?
[Discipulus]: in this ubuntu i'm not able to find resource monitor, graphical mount program.. just office and amazon are exposed!

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2016-12-07 08:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (125 votes). Check out past polls.