You have a few problems here that we can clear up. First, you need to use warnings. Either include -w on the shebang line of your script:
#!/usr/bin/perl -w
Or use the "warnings" pragma if your version of Perl supports it:
use warnings;
These will tell you that "$main::str" was used only once, which can help you see that you have a typo in the following line:
$str = s/.*\-D*?//;
Further, 'use strict;' will catch errors like that.

You probably meant for $str to be $_. Also, you have an equals '=' sign when you want the regex binding operator: '=~'. The reason you are getting output is because your regular expression is evaluating against the value of $_ and you are throwing away the value of $str. You can either leave off the '$str =' part, or change the code to the following:

$_ = "text1 text2 -f -DFLAG1 -global -DFLAG2 -DFLAG1"; s/.*?(?=-D)//; print "$_\n";
The (?=-D) uses a positive lookahead. That checks to see if it matches data, but doesn't include that data in the text that is matched. See perlre for more information on this.

Also, I don't like the dot star in this regular expression. If your data is likely to always be in this format, try the following:

s/[^-]+-[^-]+//;
However, that regex looks a bit obscure. If this is not a time-sensitive script, you may want to stick with the first one. See Death to Dot Star! for information on problems with the dot star '.*' combination in regexes.

That regular expression, broken out with the /x modifier, reads like this:

s/ [^-]+ # Negated character class - match one or more # of anything that is not a dash: "text1 text2 " - # match the dash: "-" [^-]+ # Negated character class - match one or more # of anything that is not a dash: "f " //x;
No offense, but you seem to have some troubles with English. Since this answer is a bit complicated, feel free to e-mail me at poec@yahoo.com if you have any questions.

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.


In reply to (Ovid) Re: Regular Expression by Ovid
in thread Regular Expression by ashok

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.