• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 426
  • Last Modified:

Regular expression for code formatting

Please tell me how I can write a regular expression such that every open- and close-parentheses has exactly one space character before and after it. If there is any other whitespace around the paren, it's collapsed into a single space character.

For instance, this code:
foo (bar ( new Point(x, graph.getY()) ));

Would be modified to look like this:
foo ( bar ( new Point ( x, graph.getY ( ) ) ) ) ;

Thanks!
0
dshrenik
Asked:
dshrenik
  • 6
  • 3
  • 2
  • +1
4 Solutions
 
Terry WoodsIT GuruCommented:
Try a regular expression replace with pattern "\s*\(\s*" and replacement " ( "
and a 2nd replace with pattern "\s*\)\s*" and replacement " ) "
0
 
Shinesh PremrajanEngineering ManagerCommented:
(\s\(\s)(.*)(\s\)\s)


Hope this helps

0
 
dshrenikAuthor Commented:
If possible, can you give a brief description of the regex u suggested? Thanks!
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
Terry WoodsIT GuruCommented:
You can probably even do it with one replacement, with pattern "\s*(?:(\()|(\)))\s*" and replacement " $1$2 "
0
 
Shinesh PremrajanEngineering ManagerCommented:
\s is for space
we cant user ( as it is so need to be esaceped hence \( is used
and then a space i.e \s
(.*) means any character.

hope this helps
0
 
Terry WoodsIT GuruCommented:
\s matches a single space character, and the * after it indicates match as many as possible of them (or none).
0
 
dshrenikAuthor Commented:
Can you relate it to "(\s\(\s)(.*)(\s\)\s)" and "\s*(?:(\()|(\)))\s*"  .... " $1$2 "

Thanks!
0
 
Terry WoodsIT GuruCommented:
My 2nd suggestion uses a non-capturing group (?:blah) to limit the effect of the | operator (logical OR), and 2 capturing groups (surrounded with round brackets () ), and uses the captured text in the replacement ($1 for the contents of the first capturing group, and $2 for the second).
0
 
Terry WoodsIT GuruCommented:
Does that make sense?
0
 
dshrenikAuthor Commented:
@TerryAtOpus:
So we are replacing a string starting with 0 or more spaces followed by either a '(' or a ')', and replace it with a space followed by '(' or ')'?

Why cant we have just 1 non-capturing group that refers to '( | )'?

Thanks!
0
 
käµfm³d 👽Commented:
I think you could simplify TerryAtOpus' pattern even further:

Find:

*([()]) *

% Note there is a space before each star above

Replace:

 $1

% Note there is a space before and after the "$1" above. Also, your language/engine may use the following syntax for replacement instead:

 \1
0
 
Terry WoodsIT GuruCommented:
Why didn't I see that?! Thanks kaufmed...
0
 
käµfm³d 👽Commented:
>>  Why didn't I see that?!

Hmmmm...   Perhaps you were up coding really late the night before, you drank your last [insert energy drink of choice here] at around 2:00 AM, and the eye crust and lack-of-sleep-dementia was derailing your internal regex engine.

Ahh, the glory days    = )
 

There is one caveat to the approach I propose in that there will actually be two spaces in between parentheses (e.g. getY ( ) would turn into getY (  ) ). That could be followed up with a replacement of

Find:
\(  \)

Replace:
( )

 to correct, though.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 6
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now