You could write a Regex to give you some text around the match doing by
doing something like
Regex MatchWithContext = new Regex
("(?<ContextBeforeMatch>.{10})(?<ActualMatch>Strin gYouWantTofind)(?<ContextAfterMatch>.{10})")
This would allow you to capture three groups. An "ActualMatch" group, a
"ContextBeforeMatch" group of 10 characters, and a "ContextAfterMatch"
group of 10 characters. I am almost certain this would work the way you want
it in the middle of the string, but you would not match anything fewer than
10 characters from the beginning or end of the string. I think that you
could fix that as follows
Regex MatchWithContext = new Regex
("(?<ContextBeforeMatch>.{0,10})(?<ActualMatch>Str ingYouWantTofind)(?<ContextAfterMatch>.{0,10})")
to allow the context groups to be as small as zero if needed, but I would
defiantly test this one before using it...
Ethan
Ethan Strauss Ph.D.
Bioinformatics Scientist
Promega Corporation
2800 Woods Hollow Rd.
Madison, WI 53711
608-274-4330
800-356-9526
et***********@promega.com
<ke*****@nospam.nospamwrote in message
news:en**************@TK2MSFTNGP04.phx.gbl...
I'm analyzing large strings and finding matches using the Regex class. I
want to find the context those matches are found in and to display
excerpts of that context, just as a search engine might. In terms of code,
what's the easiest way to make that happen? The code below works fine for
identifying the matches, but it doesn't try to extract the surrounding
context or display it:
currPageText = [method which grabs text from my source]
numberOfMatches = Regex.Matches(currPageText,
pattern,RegexOptions.IgnoreCase).Count;
Response.Write("found " + numberOfMatches + "<br>");
Thank you,
-KF