greg_roberts
asked on
C# Regex Query
I am after a leg up with the following in C#.
I want to replace various names / alias for one common name.
The input string is one string which contains multiple lines in it. Search to be case insentive.
e.g. master word "Fred Project" to replace :
"Fred." or "Fred. " or "Fred " (Fred space) but not the Fred in "Fred Project" also NOT "Fred.txt"
(i.e. if Fred is stand alone or at the end of a sentence, then replace, if fred is part of something else, don't)
Freddy
the project
project fred
project freddy
I don't care if i have to loop a loop for each name or if the expression can handle the lot.
Regex.Replace() is what i am using.
I have it kind of working but don't have the syntax right for the replacement, e.g.
Freddy considerations => Fred Projectconsiderations (WRONG)
want
Freddy considerations => Fred Project considerations (RIGHT)
That is because although my match works (Freddy(\s?)) the system thinks my replacement string needs to include the extra whitespace characters...
Thanks!
I want to replace various names / alias for one common name.
The input string is one string which contains multiple lines in it. Search to be case insentive.
e.g. master word "Fred Project" to replace :
"Fred." or "Fred. " or "Fred " (Fred space) but not the Fred in "Fred Project" also NOT "Fred.txt"
(i.e. if Fred is stand alone or at the end of a sentence, then replace, if fred is part of something else, don't)
Freddy
the project
project fred
project freddy
I don't care if i have to loop a loop for each name or if the expression can handle the lot.
Regex.Replace() is what i am using.
I have it kind of working but don't have the syntax right for the replacement, e.g.
Freddy considerations => Fred Projectconsiderations (WRONG)
want
Freddy considerations => Fred Project considerations (RIGHT)
That is because although my match works (Freddy(\s?)) the system thinks my replacement string needs to include the extra whitespace characters...
Thanks!
How about:
Find:
(?i)\bfred(?!\sproject)(?!\.\S)\b
Replace:
Fred Project
If neither solution works for you, could you please provide sample inputs with expected outputs of the form:
input = output
input = output
.
.
.
input = output
The format is really arbitrary, but we are just looking to see *exactly* what you expect to receive. I'm not sure about others, but I was a little confused by the description of expected output above :)
input = output
input = output
.
.
.
input = output
The format is really arbitrary, but we are just looking to see *exactly* what you expect to receive. I'm not sure about others, but I was a little confused by the description of expected output above :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks All
matthewspatrick - This didn't cover the fred.txt case and i was going to do a follow up for an explanation but others have covered.
Mathiyazhagan - Brillant response, explanation and an example. You should write msdn doco !!
Your code works!! Love the way you did an exampel to prove it.
I have to be careful in the order of my word "patternsFind" as i can end up with double up. Once i changed the order all fine.
NB: string newpattern =string.Format("{0}?{1}{2} +{0}?",@"( project\s) "
My reference to not replacing the "Fred" in "Fred Project" was a generic reference to avoid getting a "Fred Project Project" effect. The "project" could be any term.
What i was trying to say is when you find the replacepattern already in the string then either ignore or replace completely (pref. the later cause this corrects the wrong case situations, e.g. "fred Project").
In another replace example the "Fred Project" master term example might be "John" or "Project Fred".
So in this context, what does newpattern look like ?
The thing i was struggling with was how to say "xxx. " "xxx yyy" ok but "xxx.yyy" no
I think this is what your commonpattern does.
Thanks again
matthewspatrick - This didn't cover the fred.txt case and i was going to do a follow up for an explanation but others have covered.
Mathiyazhagan - Brillant response, explanation and an example. You should write msdn doco !!
Your code works!! Love the way you did an exampel to prove it.
I have to be careful in the order of my word "patternsFind" as i can end up with double up. Once i changed the order all fine.
NB: string newpattern =string.Format("{0}?{1}{2}
My reference to not replacing the "Fred" in "Fred Project" was a generic reference to avoid getting a "Fred Project Project" effect. The "project" could be any term.
What i was trying to say is when you find the replacepattern already in the string then either ignore or replace completely (pref. the later cause this corrects the wrong case situations, e.g. "fred Project").
In another replace example the "Fred Project" master term example might be "John" or "Project Fred".
So in this context, what does newpattern look like ?
The thing i was struggling with was how to say "xxx. " "xxx yyy" ok but "xxx.yyy" no
I think this is what your commonpattern does.
Thanks again
ASKER
p.s. I think
string newpattern = string.Format("{0}{1}+", name, commonpattern);
is the generic form ?
string newpattern = string.Format("{0}{1}+", name, commonpattern);
is the generic form ?
Hi,
Glad to see that I helped you .
>> string newpattern = string.Format("{0}{1}+", name, commonpattern); - is the generic form ?
yes, you can use it as generic form with some more extra delimeters in common pattern.
Thanks for your compliments.Thank you.
Glad to see that I helped you .
>> string newpattern = string.Format("{0}{1}+", name, commonpattern); - is the generic form ?
yes, you can use it as generic form with some more extra delimeters in common pattern.
Thanks for your compliments.Thank you.
(fred[a-z]*( +project)?)(?=$|[ \.,!\?:;])
with replacement text of:
Fred Project