Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Regular expression for code formatting

Posted on 2011-02-23
13
Medium Priority
?
420 Views
Last Modified: 2012-05-11
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
Comment
Question by:dshrenik
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
  • +1
13 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 1000 total points
ID: 34967227
Try a regular expression replace with pattern "\s*\(\s*" and replacement " ( "
and a 2nd replace with pattern "\s*\)\s*" and replacement " ) "
0
 
LVL 17

Assisted Solution

by:Shinesh Premrajan
Shinesh Premrajan earned 500 total points
ID: 34967228
(\s\(\s)(.*)(\s\)\s)


Hope this helps

0
 

Author Comment

by:dshrenik
ID: 34967235
If possible, can you give a brief description of the regex u suggested? Thanks!
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 1000 total points
ID: 34967238
You can probably even do it with one replacement, with pattern "\s*(?:(\()|(\)))\s*" and replacement " $1$2 "
0
 
LVL 17

Expert Comment

by:Shinesh Premrajan
ID: 34967241
\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
 
LVL 35

Expert Comment

by:Terry Woods
ID: 34967243
\s matches a single space character, and the * after it indicates match as many as possible of them (or none).
0
 

Author Comment

by:dshrenik
ID: 34967254
Can you relate it to "(\s\(\s)(.*)(\s\)\s)" and "\s*(?:(\()|(\)))\s*"  .... " $1$2 "

Thanks!
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 34967255
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
 
LVL 35

Expert Comment

by:Terry Woods
ID: 34967262
Does that make sense?
0
 

Author Comment

by:dshrenik
ID: 34967276
@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
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 500 total points
ID: 34969501
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
 
LVL 35

Expert Comment

by:Terry Woods
ID: 34974113
Why didn't I see that?! Thanks kaufmed...
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34974734
>>  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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question