Raghupathy wrote: > Rob wrote: > > Raghupathy wrote: > > > Hi All, > > > > > > I have a text file as shown below. How do I match > > > patterns like the following: > > > > > > Pattern to be matched: > > > ========================= > > > #ifndef def > > > ...... (anything except #if) > > > #else def > > > ....... (anything except #if) > > > #endif def > > > > > > > > > My Input Data: > > > > > ===================================================== > > > #ifndef def > > > DELETE sys1..tbl1 > > > #endif def > > > > > > SELECT col1, col2, col3 > > > #ifndef def > > > FROM sys1..tbl1 > > > #else def > > > FROM sys1..tbl2 > > > #endif def > > > WHERE schdid is not null > > > > > > > > ===================================================== > > > > > > What I tried is below (but did not work since it > > > captured patterns which had "#if" nested within the > > > main pattern). > > > > > > $line1 =~ > > > m/#ifndef\s+def(.*?)#else\s+def(.*?)#endif\s+def/isg > > > > > > > I thought I knew what you meant, but then I changed my > > mind! > > > > If your input file is, say > > > > statement1; > > #ifdef def1 > > statement2; > > #ifdef def2 > > statement3; > > #else > > statement4; > > #endif > > statement5; > > #else > > statement6; > > #ifdef def3 > > statement7; > > #else > > statement8; > > #endif > > statement9; > > #endif > > > > What is it that you want to capture? > > > The input file you described is not correct, since > it has > #ifdef def2 > statement3; > #else > statement4; > #endif > nested within another #ifdef ... #else ... #endif. > > My input file is the output of > "diff -D def file1 file2" (on unix). This will > generate a file which has the following patterns and > none of the patterns can be nested within the other: > > #ifdef .... #else ... #endif > #ifndef .... #else ... #endif > #ifdef .... #endif > #ifndef .... #endif > > I need to substitute the above patterns to be read > by a home grown program. > > I encountered a problem due to the following > reason. > There was: > > #ifndef def .... #endif def - Call it sentence1 > #ifndef def ... #else def ... #endif def - Call it > sentence2 > > I tried the following line but it matched sentence1 > and sentence2 together. I need to match sentence1 and > sentence2 seperately. > $line1 =~ > m/#ifndef\s+def(.*?)#else\s+def(.*?)#endif\s+def/isg > > For this > #ifndef def (...1...) #else def (...2...) #endif def > > should be matched only if #if is not there within > (...1...) and (...2...). > > Hopefully I have conveyed it more clearly.
After a lot of thought I think I know what you mean. The 'nested' in your subject threw me off, as there's no nesting involved! Essentially, you want to grab an undefined, defined pair of strings in sequences like this. #ifdef def defstring #endif #ifndef def undefstring #endif #ifdef def defstring #else undefstring #endif #ifndef def undefstring #else defstring #endif That's what the program below does. It captures 'ifdef' or 'ifndef' into $1, the first string into $2 and the second string (if any) into $3. It then uses $2 and $3 (defaulted to the null string if it is undefined) to set up scalars $def and $undef. It does this for every #if .. #endif in the supplied string. BTW please post your replies at the bottom of the messages. This is a complex thread and it gets very difficult to understand if we are responding at opposite ends of the text! I hope this helps. Rob use strict; use warnings; my $code = <<END; #ifndef def DELETE sys1..tbl1 #endif def SELECT col1, col2, col3 #ifndef def FROM sys1..tbl1 #else def FROM sys1..tbl2 #endif def WHERE schdid is not null END while ( $code =~ m{ \#(ifn?def) \s+ def \s* \n (.*?) \s+ (?: \#else \s+ def \s* \n (.*?) \s+ )?? \#endif \s+ def \s* \n }isxg ) { my ($def, $undef) = $1 eq 'ifdef' ? ($2, $3 || '') : ($3 || '', $2); printf " Defined: %s\n", $def; printf "Undefined: %s\n", $undef; print "\n"; } ** OUTPUT Defined: Undefined: DELETE sys1..tbl1 Defined: FROM sys1..tbl2 Undefined: FROM sys1..tbl1 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]