REGULAR EXPRESSION extract a word and text around it

T

teo

I have a problem (partial).

Some days ago I asked for a way
to extract a word and few text around it
(30 chars on the left and 30 on the right)
from a long text.

I went good with:

..{1,30}?myWord.{1,30}



----- here the code: -------------


' Let's assume the target word is "carcass"

Dim Input As String = "Sylvia Brunner, a marine mammals
researcher at the museum in Fairbanks, identified
the decomposing carcass and oversaw its recovery on Wednesday.
The bloated, black thing on the beach
was about 12 feet from the river's edge"

Dim Pattern As String = ".{1,30}?carcass.{1,30}"

Dim myMatch As Match = Nothing



myMatch = Regex.Match(Input, Pattern, RegexOptions.Multiline)

If myMatch.Success Then
Debug.WriteLine(myMatch.Value)
End If

----------------


BUT...


if the word is at very beginning or at very ending
of the phrase (ie: "Sylvia" or "edge" )
the RegEx fails.

How to match also these extreme cases?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top