Use Regex to identify if text is inside quotes

Posted on 2006-05-27
Last Modified: 2012-05-05
Can someone advise the Regex expression to allow replacement of text if it doesn't appear inside quotes. For example if I have the following

Dim Var1, Var2, Var3 As Integer
CallSomeMethod(Var1, Var2, Var3, "Var1", "Var2", "Var3")

How do I replace Var2 with another name, say NewVar, so that the text appears as follows -

Dim Var1, NewVar, Var3 As Integer
CallSomeMethod(Var1, NewVar, Var3, "Var1", "Var2", "Var3")
Question by:Stephen_Curle
    LVL 3

    Expert Comment

    To exclude quoted variables, your expression should exclude character 34 ascii = 22 hex

    Use the same to exclude "(", [A-Z|0-9], to avoid replacement of strings like "Function Var2(...)" or strings like Var21

    Author Comment

    I don't think this is quite what I need and the example I gave was probably too basic. What if I wanted to replace Var2 in the following

    CallSomeMethod(Var1, Var2, "Var1, Var2", "Don't replace Var2 here", Var2, "Don't replace Var2 here")

    so revised text appears

    CallSomeMethod(Var1, NewVar, "Var1, Var2", "Don't replace Var2 here", NewVar, "Don't replace Var2 here")

    The sort of thing I'm looking for (if it's possible) is to be able to right code such as

    RevisedText = Regex.Replace(Text, NotInQuotes & "Var2", "NewVar")

    where I need to know the correct Regex expression for NotInQuotes that will work in all cases.
    LVL 96

    Accepted Solution

    1) That requirement appears to be a little too complex for a single regular expression.

    2) My initial thoughts are to break that string up by comma, but you probably have more of examples of where that wouldn't work:

      "Var1, Var2"
      "Don't replace Var2 here"
       "Don't replace Var2 here"

       This way, you could check if the string starts with quote, and skip those, but do a simple Replace function.


    Author Comment

    I think you can get it to work by splitting by " and then changing every other item.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    779 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

    10 Experts available now in Live!

    Get 1:1 Help Now