By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,058 Members | 1,213 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,058 IT Pros & Developers. It's quick & easy.

REGULAR EXPRESSION extract a word and text around it

P: n/a
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?
Jun 29 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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]
ja******@online.microsoft.com
All opinions are my own. All content is provided "AS IS" with no warranties,
and confers no rights.
Jun 29 '06 #2

P: n/a
On Thu, 29 Jun 2006 16:29:04 +0000 (UTC), Jared Parsons [MSFT]
<ja******@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}
Jun 29 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.