Smarter Regex Replace

Given the following string:
SELECT * FROM myTable WHERE Description LIKE 'some text'

Open in new window

I am using a regular expression as follows:
[^[][*'][^]]

Open in new window

This easily detects an asterisk or squote character that isn't enclosed in brackets. What I'd like to do is create a single regular expression that adds square brackets around a "naked" asterisk or squote character. I could do it with two discreet regex calls but I'm hoping there's a smarter way to build the regular expression such that if it finds any character in the middle part that it is automatically enclosed in square brackets.
LVL 22
Russ SuterSenior Software DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kaufmed   ( ⚆ _ ⚆ )I asked the operating system what I could do to become a better programmer. It said, "Let me give you some pointers."Commented:
Add a capture group (i.e. parentheses):

[^[]([*'])[^]]

Open in new window


Then use that group in your replace:

string result = Regex.Replace(source, "[^[]([*'])[^]]", "[$1]");

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Russ SuterSenior Software DeveloperAuthor Commented:
Worked like a charm. I often used parentheses in regex but never understood that they meant capture groups. This expands my usage greatly. Thanks!
kaufmed   ( ⚆ _ ⚆ )I asked the operating system what I could do to become a better programmer. It said, "Let me give you some pointers."Commented:
Keep in mind that capture groups are numbered from left to right. So given the following:

(abc)(123)

Open in new window


...capture group 1 will contain "abc" and capture group 2 will contain "123". Capture groups can also be nested:

((hello) world), (it's me)

Open in new window


Numbering here is slightly trickier, but more or less the same. It's still left to right, but as you encounter a new opening paren, the numbering increases. In this case, "hello world" is capture group 1, just "hello" is in capture group 2, and "it's me" is in capture group 3.

When you want to access a capture group, then you use the syntax "\n" or "$n", where "n" is the number of the group. ("\0" and "$0" are special capture groups which represent the entire matched string, regardless of any other capture groups.) Which you use depends on where you are working. If you are trying to use the capture group within the pattern itself, then you use the slash version. If you are working within the replacement, then you use the dollar version.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.