Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

simple regexp question

by KarthikK (Sexton)
on Nov 21, 2007 at 09:23 UTC ( #652084=perlquestion: print w/ replies, xml ) Need Help??
KarthikK has asked for the wisdom of the Perl Monks concerning the following question:

Hello all,
i want to replace any string which contains "&;" a space before the semicolon
Example:

$strTest = "Sometext with cotnent K&K;";
my output should looke like this
"Sometext with cotnent K&K ;"
a space before the semicolon.
the issue is thre can be 'n' number of text or number between & and ; like

sometext with conetnst add kklkk&dfaf:dfa▄─mldd÷;

How can i write a pattern for this ?
is it possible atall?
thanks & regards
Karthik

Comment on simple regexp question
Download Code
Re: simple regexp question
by andreas1234567 (Vicar) on Nov 21, 2007 at 09:38 UTC
    Something like this perhaps?
    $ perl -w use strict; my $str = q{sometext with conetnst add kklkk&dfaf:dfa▄─mldd÷;}; $str =~ s/&(.*?);/&$1 ;/g; print $str; __END__ sometext with conetnst add kklkk&dfaf:dfa▄─mldd÷ ;
    Update: Thanks to johngg for identifying glitch.
    --
    Andreas
      Did you mean

      $str =~ s/&(.*?);/&$1 ;/g;

      perhaps? Your code as it stands seems to lose the text after the ampersand.

      Cheers,

      JohnGG

      In customising the andreas1234567 code, you can use it as

      use strict; my $str = q{sometext with conetnst add kklkk&dfaf:dfa▄─mldd÷;}; $str =~ s/&([^;]+);/&$1 ;/g; #This includes all the characters til +l the semicolon print $str;

      Punitha

Re: simple regexp question
by cdarke (Prior) on Nov 21, 2007 at 09:45 UTC
    Maybe I am reading the question differently, I would go for:
    $strTest =~ s/(&.*?);/$1 ;/g;
Re: simple regexp question
by sen (Hermit) on Nov 21, 2007 at 09:50 UTC

    i think, it works, if i understand your reqirement,

    $strTest =~ s/(.*?)(&)(.*?)(;)/$1$2$3 $4/g;
      This pattern seems to work. but i will try and do some more testing with my req. thanks a lot

      regrads
      Karthik
Re: simple regexp question
by jbert (Priest) on Nov 21, 2007 at 10:25 UTC
    And to elaborate slightly on why the given solutions work, parens ( ) in a regexp capture their contents to the special variables $1, $2, ....

    These are numbered according to the order of the capture groups (as ordered by their left hand paren).

    You can use them (as shown) in the right-hand-side of a search and replace, but they're also valid perl variables which you can make use of after a substitution or ordinary match:

    my $line = "furrfu"; $line =~ /(r+)/; print "$line contained ", length($1), " repeated 'r's\n";
    Since regexp could in principle overwrite these global special variables, you should make use of them immediately after the regexp and/or save off their values into more appropriateley named vars. (e.g. if you call a subroutine, you don't know if that sub has done a regex and overwritten them).

    Since parens are used for purposes other than grouping (e.g. to have alternating choices (foo|bar|baz), you can use a "non-capturing group", with (?:).

    Search for 'captur' in perlre for more gory details.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (13)
As of 2014-07-14 09:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (257 votes), past polls