Solved

Regular expression for code formatting

Posted on 2011-02-23
13
407 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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 74

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 74

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
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…

746 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

12 Experts available now in Live!

Get 1:1 Help Now