rajans has asked for the wisdom of the Perl Monks concerning the following question:

^\s*case\s+.*?:\s*$ My tool reads a xml file to parse the soruce code files (like java, C++ etc..) to generate the line count, the above expression in my xml file makes all the lines which has only the word " case : " to get ignored. Could someone plese help me in below two cases to write above kind of expression. CASE1:- If any line starts with : (colon), then that line should be ingored. For example, in the below line the word END stars with : , so this line should be ignored. (there is no space betwwen : and END word) :END CASE 2:- if any line starts with the word struct then that line should be ignroed. for example, below line should be ingnored struct simple

Replies are listed 'Best First'.
Re: regfular expression hlep
by CountZero (Bishop) on Jun 13, 2012 at 19:22 UTC
    Do you mean you wish to ignore all lines which either have only the string "case :" or start with a colon or start with the string "struct" and put that in one regex?

    If so, this will work:

    use Modern::Perl; while (<DATA>) { next if /^\s*case :$|^:|^struct/; print; } __DATA__ case : case : case : keep me : ignore this keep this line struct: get rid of me struct: but keep me

    By the way, your regex will also ignore lines which have anything between "case" and the colon. That does not seem to conform to your requirement.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
      <expression>^\s*case\s+.*?:\s*$</expression> the abvove lines in the xml file makes the tool to ignore all the lines which are having only the word " case : ". For example all below lines will get ignored. case 1: case 2: case ABC: now, i want the same code for ignoring the any line which starts with " : " (colon). Example, :END ---> this line should be ignored :STOP ---> this line should be ignored
        If you want three separate regexes, just split the combined one at the | symbol:

        • /^\s*case [^:]*:/ (I have amended this to conform to your examples)
        • /^:/
        • /^struct/

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        My blog: Imperial Deltronics
Re: regfular expression hlep
by rovf (Priest) on Jun 14, 2012 at 08:10 UTC