0

I have seen many answers pointing out use (?s) ahead of reg expression but it will match more than I expect. I'm using search in Eclipse Neon.

Ex:

Line 1: String[] str1 = new String[] {"a", "b",
Line 2:     "c", "d"};
Line 3: String[] str2 = new String[] {"aa", "bb",
Line 4:     "cc", "dd"};

Regex (?s)(\w+)\[\] (\w+) = new .+ (\{.*\}); will match line 1 to line 4, stops matching in the last };, while I want to make the match stop in the first }; so line1\2 and line3\4 can be matched separately.

Lee
  • 535
  • 5
  • 19
  • where is your code ? – Mohsen_Fatemi Jan 25 '17 at 19:58
  • It it the code with label Line1 to Line4. Format updated. – Lee Jan 25 '17 at 20:01
  • I removed the IDE tags and mentions. If it is an IDE specific issue, please explain how it is. – weston Jan 25 '17 at 20:03
  • I'm trying to search in Eclipse. Wondering maybe Eclipse regex engine has some specific rules so was mentioning it. If there's no difference between Eclipse regex engine and others then it has nothing to do with IDE, – Lee Jan 25 '17 at 20:11
  • The title is not correct. If you assume greedy, it will match the largest pattern as possible. What you need to perform this is a reluctant pattern. I'll explain in details and suggest further reading. – daniel souza Jan 25 '17 at 20:19
  • I can't no longer answer in this thread, but what you need to perform this is a **reluctant** pattern matching. **.+ or .** will match the biggest pattern your regex can match, it's **greedy**. **.+?** or **.*?** will break at the first match your regex can be applied. It's **reluctant**. Please consider bookmarking the (Stack Overflow Regular Expressions FAQ)[http://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean/22944075#22944075] for future reference. – daniel souza Jan 25 '17 at 20:39
  • Thanks for pointing that out and explain what is reluctant and how it works. The link is a great reference as well. Thanks! – Lee Jan 25 '17 at 21:17

1 Answers1

1

Simply putting a ? after .* fixes the problem:

(?s)(\w+)\[\] (\w+) = new [^{]+ (\{.*?\});

This matches as few characters between the {} as possible.

EDD
  • 2,070
  • 1
  • 10
  • 23
  • I copied your regex but makes no difference, still matches all 4 lines. – Lee Jan 25 '17 at 20:03
  • @Lee Really? check out this here http://regexr.com/3f5ae. It works with a few modifications to work with javascript but it should still work the same in Java. Maybe there is something else wrong. – EDD Jan 25 '17 at 20:05
  • @Lee actually sorry, my bad I copied the regex incorrectly :/ I've updated the question with the new regex. – EDD Jan 25 '17 at 20:07
  • It works, thanks! So the key is to let (?s) does not match '{' and then .*? can match the rest in non-greedy way, right? – Lee Jan 25 '17 at 20:15
  • @Lee I have no idea lol. Give it a go. – EDD Jan 25 '17 at 20:17