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

regex for removing quoted text

by Anonymous Monk
on Mar 10, 2011 at 04:35 UTC ( #892341=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am new to regex in Perl.I have following piece of lines-- "World-Keys table of different sources The content is derived from 100's of different sources. The following is a list of sources that have been assigned specific keywords for our friends." Now i need to find a regex for the above lines so as to remove occurence of all data between the double quotes including new lines. I have tried everything possible for the above lines but nothing has worked for me. Can somebody help me out, please

2011-03-10 Retitled by GrandFather, as per Monastery guidelines

Replies are listed 'Best First'.
Re: regex for removing quoted text
by roboticus (Chancellor) on Mar 10, 2011 at 04:56 UTC

    You say you've tried "everything possible", so just pick the one that gave you the correct solution. ;^)

    Seriously, show the code that you've tried. I'm sure someone can (and probably will) just give you the answer, but it's more satisfying to help someone fix their code, rather than handing you the answer. Once you put up some code, someone will be sure to give you a hand finding out where you have difficulties.

    I've recently played with YAPE::Regex::Explain, and if you try it out, you can tweak your regular expressions and have it tell you what it will do. That's a pretty good way to see where you're going wrong.

    I'm assuming that you've already read perldoc perlre, so I won't bother mentioning it.


    When your only tool is a hammer, all problems look like your thumb.

    Update: Repaired CPAN link, thanks to toolic and atcroft for the catch!

Re: regex for removing quoted text
by atcroft (Monsignor) on Mar 10, 2011 at 05:02 UTC

    You want to look at the /s switch on the s/// operator. For instance, with the following code (in a one-liner with -l):

    my $str = q{this is "a test} . qq{\n} . q{this is "only a "poor "test}; print $str; print qq{=====}; $str =~ s/"[^"]+"//gs; print $str;
    I get the following result, which is similar to what you indicated you needed:

    Hope that helps.

    Update: 2011-03-09

    I cannot help but second roboticus's references and suggestions. There is much useful information (and I now have another module to take a look at when I have "spare" time available again). :)

    Update: 2011-03-09

    It appears as if the module in question may be YAPE::Regex::Explain (as I found when I tried installing it to play with it myself).

      You want to look at the /s switch on the s/// operator.

      $str =~ s/"[^"]+"//gs;

      The  /s option has no effect in the expression /"[^"]+"/, it is superfluous.

        Technically, but it still signals the authors intentions
Re: regex for removing quoted text
by DrHyde (Prior) on Mar 10, 2011 at 11:05 UTC
    There are several things you can do to make it more likely that you'll get useful responses:
    1. Most importantly, use a descriptive title. Most users will only see the title before choosing (or not) to look at your question. Right now there's no indication from the title whether you have an interesting problem or not, so most people will probably assume that you don't;
    2. Format your post so that literal text (ie the three lines in between quotes) appears exactly as you intend. I had to view source to see what the hell you were talking about;
    3. Show us what you've already tried, and instead of just saying "nothing worked" explain how the behaviour of what you've tried differs from what you expected and from what you wanted;
    4. Grow a fabulous moustache. Stalin had a fabulous moustache, and everyone was eager to help him, so maybe it'll work for you too!

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2017-10-21 03:58 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (269 votes). Check out past polls.