0

I have a program where I'm trying to compare two files.

File 1:

Void Function( Col1,
               Col2,
               Col3 )

File 2:

Void Function(Col1,Col2,Col3)

What regexp should I use to remove the spaces and enter between ( and ) since I only want to reduce space between brackets and not any other place in my code.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • possible duplicate of [Whitespace in Java's regular expression](http://stackoverflow.com/questions/10521521/whitespace-in-javas-regular-expression) – Smutje Jun 27 '14 at 06:23
  • 1
    @Smutje: Those two questions aren't even remotely related. – Tim Pietzcker Jun 27 '14 at 06:24
  • @Smutje I don't see it as a duplicate... It's an Exclusion question (only replace there, but not in the rest of his code. What do you think ? – zx81 Jun 27 '14 at 06:25
  • I see, we need more guidance: http://stackoverflow.com/questions/4731055/whitespace-matching-regex-java – Smutje Jun 27 '14 at 06:26

1 Answers1

2

This is problematic. Any pure Java regex solution will have to compromise because to correctly assess the question "are we between parentheses?" requires us (in the worst case, depending on your input) to handle nested parentheses, text within strings or comments that should not be modified and possibly other edge conditions.

Keeping that in mind, a simplistic solution that works on your example at least (and that assumes that it doesn't have to handle nested parentheses, comments or strings, and that it doesn't have to check whether the parentheses actually belong to a function definition), could be

String result = subject.replaceAll("\\s+(?=[^()]*\\))", "");

This matches and replaces whitespace iff the next parenthesis after it is a closing parenthesis.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • If I recognize the problem to solve, it’s getting a diff between two copies of the same code where one has functions’ parameters on multiple lines but the other piece may not, then a two-pass approach is probably the only complete solution. I’ve had to do this before, and it isn’t much fun; using ad-hoc regexes to parse a programming language that has an actual grammar exposes you to false positives in strings or comments. My solution was a prepass to join up the lines into one, for which a [recursive pattern for balanced parens](http://stackoverflow.com/a/4034386) was needed. – tchrist Jun 29 '14 at 13:30