Link to home
Start Free TrialLog in
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'
Avatar of effes
Flag of Germany image

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").
Avatar of kaufmed
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of mmalik15


Perfect Buddy!