[Closed] Replacing text depending on the match
After some days fighting with files reading, files writing, finding words, RegEx… I have reached a function which may be useful to others.
Here it is. It changes all the matched words in a string text (based in a RegEx pattern) with something depending on the matched word itself.
There are two functions already defined with a default pattern:
- ‘CapText’ that converts the first character of each match to uppercase (default pattern: all alphabetical words) – Function copied from the MSDN examples.
- ‘AddZero’ that adds a “0” at the end of each match (default pattern: all numbers finished with a dot “.” character, without decimals).
At the end of the code there are some examples of use.
It would be great if the CGTalk community (you who are reading this!) can incorporate other usefull functions.
-----------------------------------------------------------------------
-- REPLACE TEXT DEPENDING ON THE MATCH by aaandres
(
source = "
using System;
using System.IO;
using System.Text.RegularExpressions;
class P3D_RegExReplace
{
public static string thePattern = \"\";
// Function to return the requested MatchEvaluator
static public MatchEvaluator evaluatorCreation(string evaluatorType, string pattern)
{
if (evaluatorType.ToLower() == \"captext\")
{
if (pattern.Equals(\"\", StringComparison.Ordinal))
{
thePattern = @\"\w+\";
}
else
{
thePattern = pattern;
}
return new MatchEvaluator(CapText);
}
if (evaluatorType.ToLower() == \"addzero\")
{
if (pattern.Equals(\"\", StringComparison.Ordinal))
{
thePattern = @\"(\b[\d]+\.[\s
])|(\b[\d]+\.$)\";
}
else
{
thePattern = pattern;
}
return new MatchEvaluator(AddZero);
}
return new MatchEvaluator(DoNothing);
}
// MatchEvaluators DEFINITIONS
//
// 1) Do nothing
static public string DoNothing(Match m)
{
return m.ToString();
}
// 2) Replace first letter with a cap letter in each match
static public string CapText(Match m)
{
string x = m.ToString();
if (char.IsLower(x[0]))
{
return char.ToUpper(x[0]) + x.Substring(1, x.Length - 1);
}
return x;
}
// 3) Add a zero at the end of each match
static public string AddZero(Match m)
{
string x = m.ToString();
if (x.Length < 2) return x;
if (x[x.Length - 1] == '.')
{
return (x + \"0\");
}
if (x[x.Length - 1] == ' ' || x[x.Length - 1] == (char)9)
{
return (x.Substring(0, x.Length - 1) + \"0\" + x[x.Length - 1]);
}
return x;
}
// MAIN FUNCTION. Find and Replace following criteria
static public string P3D_replaceText(string text, string evaluatorType, string pattern)
{
MatchEvaluator evaluator = evaluatorCreation(evaluatorType, pattern);
string result = Regex.Replace(text, thePattern, evaluator);
return result;
}
}
"
global FileAssembly
fn CreateFileAssembly source instanceName forceRecompile:on =
(
if forceRecompile or not iskindof ::FileAssembly dotnetobject or (::FileAssembly.GetType()).name != "RuntimeAssembly" do
(
csharpProvider = dotnetobject "Microsoft.CSharp.CSharpCodeProvider"
compilerParams = dotnetobject "System.CodeDom.Compiler.CompilerParameters"
compilerParams.ReferencedAssemblies.Add("System.dll");
compilerParams.GenerateInMemory = true
compilerResults = csharpProvider.CompileAssemblyFromSource compilerParams #(source)
FileAssembly = compilerResults.CompiledAssembly
FileAssembly.CreateInstance instanceName
)
)
global P3D_RegExReplace = CreateFileAssembly source "P3D_RegExReplace"
global P3D_Replace = P3D_RegExReplace.P3D_replaceText
)
-- How to use:
/*
P3D_Replace "aaandres carmen pilar" "captext" @"\w+" -- Gets "Aaandres Carmen Pilar"
P3D_Replace "aaandres carmen pilar" "captext" "" -- Standar way
P3D_Replace "1.0 5. 38.25 25.1 74. 7." "addzero" @"(\b[\d]+\.[\s
])|(\b[\d]+\.$)" -- Gets "1.0 5.0 38.25 25.1 74.0 7.0"
P3D_Replace "1.0 5. 38.25 25.1 74. 7." "addzero" "" -- Standar way
P3D_Replace "aaandres carmen pilar" "addzero" @"\w+\s" -- Gets "aaandres0 carmen0 pilar"
P3D_Replace (P3D_Replace "aaandres carmen pilar" "addzero" @"\w+\s") "captext" "" -- Gets "Aaandres0 Carmen0 Pilar"
*/
Added an overload with an optional fourth param to be able for example:
- To change each match by a fixed string (‘Fixed’ function)
- To add quotes of any type to each match (‘AddQuotes’ function)
(examples at the end of the code)
-----------------------------------------------------------------------
-- REPLACE TEXT DEPENDING ON THE MATCH by aaandres
(
source = "
using System;
using System.IO;
using System.Text.RegularExpressions;
class P3D_RegExReplace
{
public static string thePattern = \"\";
public static string theReplacement = \"\";
// Function to return the requested MatchEvaluator
static public MatchEvaluator evaluatorCreation(string evaluatorType, string pattern, string replacement = \"\")
{
if (evaluatorType.ToLower() == \"captext\")
{
if (pattern.Equals(\"\", StringComparison.Ordinal))
{
thePattern = @\"\w+\";
}
else
{
thePattern = pattern;
}
return new MatchEvaluator(CapText);
}
if (evaluatorType.ToLower() == \"addzero\")
{
if (pattern.Equals(\"\", StringComparison.Ordinal))
{
thePattern = @\"(\b[\d]+\.[\s
])|(\b[\d]+\.$)\";
}
else
{
thePattern = pattern;
}
return new MatchEvaluator(AddZero);
}
if (evaluatorType.ToLower() == \"fixed\")
{
if (pattern.Equals(\"\", StringComparison.Ordinal))
{
thePattern = @\"\w+\";
}
else
{
thePattern = pattern;
}
theReplacement = replacement;
return new MatchEvaluator(Fixed);
}
if (evaluatorType.ToLower() == \"addquotes\")
{
if (pattern.Equals(\"\", StringComparison.Ordinal))
{
thePattern = @\"\w+\";
}
else
{
thePattern = pattern;
}
theReplacement = replacement;
return new MatchEvaluator(AddQuotes);
}
return new MatchEvaluator(DoNothing);
}
// MatchEvaluators DEFINITIONS
//
// 1) Do nothing
static public string DoNothing(Match m)
{
return m.ToString();
}
// 2) Replace first letter with a cap letter in each match
static public string CapText(Match m)
{
string x = m.ToString();
if (char.IsLower(x[0]))
{
return char.ToUpper(x[0]) + x.Substring(1, x.Length - 1);
}
return x;
}
// 3) Add a zero at the end of each match
static public string AddZero(Match m)
{
string x = m.ToString();
if (x.Length < 2) return x;
if (x[x.Length - 1] == '.')
{
return (x + \"0\");
}
if (x[x.Length - 1] == ' ' || x[x.Length - 1] == (char)9)
{
return (x.Substring(0, x.Length - 1) + \"0\" + x[x.Length - 1]);
}
return x;
}
// 4) Change all matchs with a fixed value
static public string Fixed(Match m)
{
return theReplacement;
}
// 5) Add quotes to each matched word
static public string AddQuotes(Match m)
{
string x = m.ToString();
if (theReplacement.Length >= 2)
{
string openQuote = theReplacement.Substring(0, 1);
string closeQuote = theReplacement.Substring(1, 1);
return (openQuote + m + closeQuote);
}
return x;
}
// MAIN FUNCTION. Find and Replace following criteria
static public string P3D_replaceText(string text, string evaluatorType, string pattern)
{
MatchEvaluator evaluator = evaluatorCreation(evaluatorType, pattern);
string result = Regex.Replace(text, thePattern, evaluator);
return result;
}
static public string P3D_replaceText(string text, string evaluatorType, string pattern, string replacement)
{
MatchEvaluator evaluator = evaluatorCreation(evaluatorType, pattern, replacement);
string result = Regex.Replace(text, thePattern, evaluator);
return result;
}
}
"
global FileAssembly
fn CreateFileAssembly source instanceName forceRecompile:on =
(
if forceRecompile or not iskindof ::FileAssembly dotnetobject or (::FileAssembly.GetType()).name != "RuntimeAssembly" do
(
csharpProvider = dotnetobject "Microsoft.CSharp.CSharpCodeProvider"
compilerParams = dotnetobject "System.CodeDom.Compiler.CompilerParameters"
compilerParams.ReferencedAssemblies.Add("System.dll");
compilerParams.GenerateInMemory = true
compilerResults = csharpProvider.CompileAssemblyFromSource compilerParams #(source)
FileAssembly = compilerResults.CompiledAssembly
FileAssembly.CreateInstance instanceName
)
)
global P3D_RegExReplace = CreateFileAssembly source "P3D_RegExReplace"
global P3D_Replace = P3D_RegExReplace.P3D_replaceText
)
-- How to use:
/*
P3D_Replace "aaandres carmen pilar" "captext" @"\w+" -- Gets "Aaandres Carmen Pilar"
P3D_Replace "aaandres carmen pilar" "captext" "" -- Standar way
P3D_Replace "1.0 5. 38.25 25.1 74. 7." "addzero" @"(\b[\d]+\.[\s
])|(\b[\d]+\.$)" -- Gets "1.0 5.0 38.25 25.1 74.0 7.0"
P3D_Replace "1.0 5. 38.25 25.1 74. 7." "addzero" "" -- Standar way
P3D_Replace "aaandres carmen pilar" "addzero" @"\w+\s" -- Gets "aaandres0 carmen0 pilar"
P3D_Replace (P3D_Replace "aaandres carmen pilar" "addzero" @"\w+\s") "captext" "" -- Gets "Aaandres0 Carmen0 Pilar"
P3D_Replace "aaandres carmen pilar" "fixed" "" "Hello" -- Gets "Hello Hello Hello"
P3D_Replace "aaandres carmen pilar" "fixed" @"c\w+" "Hello" -- Gets "aaandres Hello pilar"
P3D_Replace "aaandres carmen pilar" "addQuotes" "" "()" -- Gets "(aaandres) (carmen) (pilar)"
P3D_Replace "aaandres carmen pilar" "addQuotes" @"c\w+" "()" -- Gets "aaandres (carmen) pilar"
P3D_Replace "aaandres carmen pilar" "addQuotes" @"c\w+" "[]" -- Gets "aaandres [carmen] pilar"
*/
what is a goal of this work?
is it a getting a library of useful regex, conversions, word validations, etc.?
The RegEx community is big. you can find almost everything including a super-duper-crazy stuff.
Hi DenisT.
I’ve searched in this forum for ‘MatchEvaluator’ and haven’t found anything. All uses of replacing I’ve found are with a fixed string. So I thought it may be interesting. That is the goal of this thread.
The main goal, what I’m looking for, is what you see in this image:
It’s taking me a long time to code an obfuscation+encryption utility, but I’m learning a lot in the path.
BTW, if any of the regular users who offer help to others is interested and wants to collaborate (up to 10 users more or less), I’ll be pleased to share the code. In the affirmative, please contact by p.m.
some time ago I have written my own ‘uglyfier’:
just I want to compare whose it’s more ugly
http://forums.cgsociety.org/showpost.php?p=7937304&postcount=11