Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

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

$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

Replies are listed 'Best First'.
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.

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

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.
      Did you mean

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

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



      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;


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://652084]
Approved by andreas1234567
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2018-06-18 08:26 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (109 votes). Check out past polls.