Avatar of mmalik15
 asked on

how to write regular expression to process a string

using regular expressions or .net string processing,  in a string like below how can we find if there is a word "in" present in string outside the parenthesis. e.g. in the the string below "in" is present  outside the parenthesis and replace it

format=xml&cfg=nescorp&q=health in medicine AND (mrtype:"Board minute" OR mrtype:"Case study" OR mrtype:"Course" OR mrtype:"Event" OR mrtype:"Initiative" OR mrtype:"Job" OR mrtype:"Policy" OR mrtype:"Press release" OR mrtype:"Publication" OR mrtype:"Report" OR mrtype:"Web page") AND (maudiencestaff:"Allied health professionals") AND (mqualificationlevel:"Career Framework 7-9") AND (msubjectgen2:"Health and social care")&pn=0&ps=10&pm=fql&srt=-dtgeneric2'
Regular ExpressionsASP.NETC#

Avatar of undefined
Last Comment

8/22/2022 - Mon

Try this method:
private string InPresent(string input)
    string replacement = "IN";

    Regex regEx = new Regex(@"(?:[^(]|\(.*?\))*?\b(?<hit>in)\b(?:[^(]|\(.*?\))*?");
    string result = input;
    foreach (Match match in regEx.Matches(input))
        foreach (Capture cap in match.Groups["hit"].Captures)
            result = result.Remove(cap.Index, cap.Length).Insert(cap.Index, replacement);

    return result;

Open in new window

It takes the string you want to process as input. Every occurrence of "in", as a word, that's not inside a pair of parenthesis is replaced by the string defined in the variable "replacement" (in this example it is simply replaced by "IN").

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

kaufmed is absolutely right. The regex I posted doesn't work at all as it was supposed to.
I'm sure I checked for cases like the one he posted but I must have copied the code from one of my earlier attempts. Since I already deleted my test solution I can't check that now.
Sorry for that, I didn't mean to cause any confusion.

Anyway, the one I got working wouldn't have worked in a Regex.Replace, hence I needed to to loop through the Matches and replace the strings manually as seen in the method I posted.

As far as I can see the one by kaufmed works with a simple Regex.Replace. So I would definitely use that one.

Perfect Buddy!
Your help has saved me hundreds of hours of internet surfing.