Home All Groups Group Topic Archive Search About

REGULAR EXPRESSION extract a word and text around it

Author
29 Jun 2006 2:41 PM
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?

Author
29 Jun 2006 4:29 PM
Jared Parsons [MSFT]
Hello teo,

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

If I understand your question correctly, the following regex should work

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

--
Jared Parsons [MSFT]
jared***@online.microsoft.com
All opinions are my own. All content is provided "AS IS" with no warranties,
and confers no rights.
Author
29 Jun 2006 7:43 PM
Chris Chilvers
On Thu, 29 Jun 2006 16:29:04 +0000 (UTC), Jared Parsons [MSFT]
<jared***@online.microsoft.com> wrote:

>Hello teo,
>
>> .{1,30}?myWord.{1,30}
>
>If I understand your question correctly, the following regex should work
>
>(.{1,30})?myWord(.{1,30})?

or simply

..{0,30}myWord.{0,30}