0

Suppose I have source code like the following, which by assumption is perfectly syntactically valid:

MyType basket("woven"); // initialize statement

double x = 0; // miscellaneous code

MyType bucket("plastic") ; // another initialize statement

for(int i=0; i<324; ++i ) { cout << i << "\n"; }

/* example of a disposal statement */
MyType basket(); 

What I want to do is detect the statement MyType bucket("plastic"); because there is no subsequent corresponding MyType bucket(); statement.

Using Python, I constructed the following regular expression (using both the DOTALL and MULTILINE options). But there's a problem, and I don't know what it is.

(?P<stmt>MyType\s+[a-zA-Z0-9_]+)\(\s*"|'[^"']+"|'\s*\)\s*;[^(?P=stmt)]*$

Essentially, I need to know how to use a named group (like (?P=stmt>)) and check for repeats of it.

2 Answers 2

2

Use this one:

(MyType\s+(\S+)\([^)]+\)\s*;)(?!.*MyType\s+\2\(\s*\)\s*;)

Here is the demo

Sign up to request clarification or add additional context in comments.

Comments

2
MyType\s*(\w+)\S+\s*;(?!.*?MyType\s*\1)

Try this.See demo.

http://regex101.com/r/zR2tR4/20

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.