Solved

Regular expression for code formatting

Posted on 2011-02-23
13
409 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
  • 6
  • 3
  • 2
  • +1
13 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 250 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:shinuq
shinuq earned 125 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
 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 250 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:shinuq
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 125 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
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,…
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…

947 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now