Connecting Tech Pros Worldwide Forums | Help | Site Map

Regex Question

JM
Guest
 
Posts: n/a
#1: Jul 1 '06
Hi,

I am not sure if this is the place to post a REGEX question if not, please
indicate me where i can post it?.

My question:
Given a string like: "Boston. MA. Holidays" I need to define the regular
expression to find each dot, the previous word and the word after. That is i
want to obtain:

first match: "Boston" "." "MA"
second match: "MA" "." "Holidays"

The problem is that if I use somthing like: ([^\s\.]+)(\.)(\s*[^\s\.]+) I
"cosume" MA in the first macth so I do not have a second match. That is I
obtained:

first match: "Boston" "." "MA"
and no more matches, since MA is consumed in the previous match.

Any help??

Thanks,
jaime



Jon Shemitz
Guest
 
Posts: n/a
#2: Jul 2 '06

re: Regex Question


JM wrote:
Quote:
Given a string like: "Boston. MA. Holidays" I need to define the regular
expression to find each dot, the previous word and the word after. That is i
want to obtain:
>
first match: "Boston" "." "MA"
second match: "MA" "." "Holidays"
>
The problem is that if I use somthing like: ([^\s\.]+)(\.)(\s*[^\s\.]+) I
"cosume" MA in the first macth so I do not have a second match. That is I
obtained:
>
first match: "Boston" "." "MA"
and no more matches, since MA is consumed in the previous match.
One solution is to use the Match overload that takes a search start
offset - Regex.Match (String, Int32) - for your second (and
subsequent) matches. If your second Match operation starts at the "MA"
substring's offset, it won't matter that "MA" was consumed by the
first Match.

--
Be the first to review my new book!

..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
Delphi skills make .NET easy to learn In print, in stores.
Closed Thread


Similar .NET Framework bytes