Home All Groups Group Topic Archive Search About
Author
30 Jun 2006 12:09 PM
JM
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

Author
1 Jul 2006 2:38 AM
Chris Chilvers
Show quote Hide quote
On Fri, 30 Jun 2006 14:09:55 +0200, "JM" <j***@ya.com> wrote:

>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
>

Could a simpler implementation not be to split by ".", then you will
have an array of strings that you can just move through a pair at a time
running the trim function to remove whitespace.

Example
========

string[] parts = full_string.Split('.');

if (parts.length > 2) {
    string a = parts[0].Trim();
    string b = parts[1].Trim();

    DoSomething(a, b);

    for (i = 2; i < parts.Length; i++) {
        a = b;
        b = parts[i].Trim();       

        DoSomething(a, b);
    }
}

========
Author
1 Jul 2006 7:11 AM
JM
Thanks Chris.
But actually I need to do it using Regular Expressions.


Show quoteHide quote
"Chris Chilvers" <kee***@dynafus.com> escribió en el mensaje
news:1onba2hdvrg2o1rcla5jgp8turh1h1nv63@4ax.com...
> On Fri, 30 Jun 2006 14:09:55 +0200, "JM" <j***@ya.com> wrote:
>
>>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
>>
>
> Could a simpler implementation not be to split by ".", then you will
> have an array of strings that you can just move through a pair at a time
> running the trim function to remove whitespace.
>
> Example
> ========
>
> string[] parts = full_string.Split('.');
>
> if (parts.length > 2) {
>    string a = parts[0].Trim();
>    string b = parts[1].Trim();
>
>    DoSomething(a, b);
>
>    for (i = 2; i < parts.Length; i++) {
>        a = b;
>        b = parts[i].Trim();
>
>        DoSomething(a, b);
>    }
> }
>
> ========