0

How can I with regex find string "executeQuery" but not if whole string has "vo" or "Viewobject".

Examples:

ResultSet rs = ps.executeQuery(); --> CORRECT
super.executeQueryForCollection(object, object1, i); --> CORRECT
voOEPoDEStruktura.executeQuery(); --> WRONG
ViewObject.executeQuery(); --> WRONG
1
  • Is there a specific reason why this must be accomplished by a regular expression? Commented Jan 12, 2012 at 14:05

2 Answers 2

3

You don't necessarily need a regex. You can use contains instead. Your condition would be:

str.contains("executeQuery") && !(str.contains("vo") || str.contains("Viewobject"))
Sign up to request clarification or add additional context in comments.

3 Comments

+1. Remember: if at first you have a problem and then you decide to solve it using regular expressions, you will suddenly have two problems. Don't over-engineer.
a bit modification to make it faster !str.contains("vo") && !str.contains("Viewobject") && str.contains("executeQuery")
I don't know if that's faster. Provided that majority of lines contain none of these strings, let's examine what happens when parsing the random 50 char line: !str.contains("vo") && !str.contains("Viewobject") && 0str.contains("executeQuery") Scan 50 chars to find the string contains no vo, scan 50 chars to find the string contains no ViewObject, scan 50 chars to see if string contains executeQuery str.contains("executeQuery") && !(str.contains("vo") || str.contains("Viewobject")) Scan the line for executeQuery... and fail 99% of the time, and finish right there.
1

^(?!.*(?:vo|ViewObject)).*executeQuery.* is the regex which will match the specifications. I didn't use capturing groups because you didnt specify that you want to capture anything.

You should use the solution with contains because it's easier to understand.

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.