We help IT Professionals succeed at work.

how to write regular expression to process a string

mmalik15
mmalik15 asked
on
586 Views
Last Modified: 2012-06-06
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'
Comment
Watch Question

Commented:
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").
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
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.

Author

Commented:
Perfect Buddy!
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.