|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
REGULAR EXPRESSION extract a word and text around itSome 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? 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. On Thu, 29 Jun 2006 16:29:04 +0000 (UTC), Jared Parsons [MSFT]
<jared***@online.microsoft.com> wrote: >Hello teo, or simply> >> .{1,30}?myWord.{1,30} > >If I understand your question correctly, the following regex should work > >(.{1,30})?myWord(.{1,30})? ..{0,30}myWord.{0,30} |
|||||||||||||||||||||||