No, regex IS slow, even with compiled your only knocking say 10% perf off on it.. I remember back in .NET 1.0, regex was about 2-3 times slower than java's implementation, and generally slower than 'programatic' format parsing..
It isn't that regex shouldn't be used.. I'm sure it has its places.. but people jump for regex at any sign of pattern parsing.. I'm sure that most of the cases could be scrapped for faster code based alternatives..
-
-
IceFreak2000 wrote: Regex regex = new Regex(@\b(?<word>\w+)\s+(\k<word>)\b",
RegexOptions.Compiled | RegexOptions.IgnoreCase);
Now think about how much harder it would be to do the same thing without regular expressions.
I propose, how about we have a verbose RegEx instead of the cryptic characters.
VerboseRegex re = new VerboseRegex(sentence, RegexOption.IgnoreCase);
foreach (string word in re.Words)
{
bool dup = re.Match(word + RegEx.WhiteSpace + word);
}
I mean, in these days of the gigabytes, let's have the computers work for us.
-
stevo_ wrote:No, regex IS slow, even with compiled your only knocking say 10% perf off on it.. I remember back in .NET 1.0, regex was about 2-3 times slower than java's implementation, and generally slower than 'programatic' format parsing..
It isn't that regex shouldn't be used.. I'm sure it has its places.. but people jump for regex at any sign of pattern parsing.. I'm sure that most of the cases could be scrapped for faster code based alternatives..
Things have improved somewhat since 1.0!
I agree though, it's not always appropriate to use regular expressions (and the original example that Minh posted is a classic example of when not to use them!)
A good example of where I've used them recently in a shipping product was with localized text strings that needed to have placeholders replaced with values reflected out of an object that contained various calculation results.
This meant I could have a string that contained something like "Available drive space {$DriveSpace:g}", and the following code would transform it (exception checking has been removed to make it more readable)
private Regex regex = new Regex(@"{\$(?<Field>[^
]*)(
?<Format>[^}]*))?}", RegexOptions.Compiled);
public static string ProcessPlaceholders(string message, object o)
{
if (!String.IsNullOrEmpty(message) && o != null)
{
message = regex.Replace(message, delegate(Match m)
{
string field = m.Groups["Field"].ToString();
string format = m.Groups["Format"].ToString();
PropertyInfo property = o.GetType().GetProperty(field);if (property != null)
{
object value = property.GetValue(o, null);IFormattable formattable = value as IFormattable;
if (formattable != null && !String.IsNullOrEmpty(format))
return formattable.ToString(format, Thread.CurrentThread.CurrentUICulture);
return value.ToString();
}
return m.ToString().ToUpper();
});
}
return message;
} -
minh- using your tablet can you draw a picture of a regular expression?
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.