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 ( [id://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

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

      regrads
      Karthik
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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-19 21:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found