J
jehugaleahsa
Hello:
I have a large input string. Instead of contantly chopping off the
front of the string (which is inefficient), I just want to maintain
the index. I then pass the index into the Regex.Match(string, int)
method.
I would like this method to stop searching as soon as it realizes that
the first character is not a match.
You can't use the ^ character, in this case, because the method does
not consider the given index as the first index in the string.
The problem with letting the pattern check past the first character is
the potential performance hit. If the first character is not a match
and the input string is long, it could take a long time for the regex
to wor through the remainder of the input. Worse, it might succeed,
but in the wrong location.
Here is a temporary implementation that inefficiently checks the
remainder of the input string.
public bool IsMatch(string input, int startingIndex, out int
length)
{
Match match = _regularExpression.Match(input,
startingIndex);
if (match.Success && match.Index == startingIndex)
{
length = match.Length;
return true;
}
else
{
length = 0;
return false;
}
}
The regular expression is defined elsewhere. This is really only a
performance problem when the input is very large, but I'd like to see
if there was a workaround.
Thanks for any pointers!
~Travis Parks
I have a large input string. Instead of contantly chopping off the
front of the string (which is inefficient), I just want to maintain
the index. I then pass the index into the Regex.Match(string, int)
method.
I would like this method to stop searching as soon as it realizes that
the first character is not a match.
You can't use the ^ character, in this case, because the method does
not consider the given index as the first index in the string.
The problem with letting the pattern check past the first character is
the potential performance hit. If the first character is not a match
and the input string is long, it could take a long time for the regex
to wor through the remainder of the input. Worse, it might succeed,
but in the wrong location.
Here is a temporary implementation that inefficiently checks the
remainder of the input string.
public bool IsMatch(string input, int startingIndex, out int
length)
{
Match match = _regularExpression.Match(input,
startingIndex);
if (match.Success && match.Index == startingIndex)
{
length = match.Length;
return true;
}
else
{
length = 0;
return false;
}
}
The regular expression is defined elsewhere. This is really only a
performance problem when the input is very large, but I'd like to see
if there was a workaround.
Thanks for any pointers!
~Travis Parks