0

I want to create regular expression which matches while(true) loop in which body is not used word 'argv'

Example 1:

    while(true){ 
        for(int i = 0; i < argc; i++) printf("%s",argv[i]);
    }

argv is used in the body of while loop - reg ex should not match this

Example 2:

 while(true){ 
     for(int i = 0; i < 5; i++) printf("%d",i);
 }

argv is not used in the body of while loop - reg ex should match this.

Till now i have just this regular expression:

while\s*\(\s*true\s*\)\s*\{ there_should_be_something \}

I dont know what to replace with there_should_be_something

Thanks a lot

3
  • 8
    Using regular expressions to parse non-regular languages like C can be rather difficult and non-rewarding. Consider using an actual parser. Commented Apr 13, 2017 at 13:23
  • @unwind i have to use reg ex Commented Apr 13, 2017 at 13:28
  • @anticol you have to use a parser Commented Apr 13, 2017 at 15:50

1 Answer 1

1

Here is a regex that works on the examples above: while\s*\(\s*true\s*\)\s*\{(([^}](?!argv))*)\}

Explanation:

  • while\s* matches while, then zero or more spaces
  • \(\s*true\s*\) matches (true) and tolerates spaces
  • \{...\} tolerates stuff between the braces
  • The stuff within the braces is based on this: [^}]*, i.e. no braces are allowed within the braces. This is a limitation for the sake simple regex. If you want to deal with all the braces, I suggest to sue a real parser.
  • This stuff within the braces is combined with negative lookahead: (?!argv) which means anything that's not followed by argv.
  • I.e. [^}](?!argv) means any non-brace character that's not followed by argv.
  • We just group it and require this group zero or more times: ([^}](?!argv))*
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.