From: Adhal on
On 20/05/2010 11:33, Tony Johansson wrote:
> "Adhal"<user(a)example.net> skrev i meddelandet
> news:eW50RbA%23KHA.1888(a)TK2MSFTNGP05.phx.gbl...
>> On 20/05/2010 11:16, Tony Johansson wrote:
>>> "Willem van Rumpt"<nothing(a)nowhere.com> skrev i meddelandet
>>> news:e$Bg2MA%23KHA.5464(a)TK2MSFTNGP05.phx.gbl...
>>>> Tony Johansson wrote:
>>>>> Hi!
>>>>>
>>>>> Here I say minimum 1 o and maximum three o but here I have more then
>>>>> three o and this expression give true but
>>>>> it should give false according to me ?
>>>>>
>>>>> bool status = Regex.IsMatch("foooooood", "o{1,3}");
>>>>>
>>>>> //Tony
>>>>
>>>> Because your criteria has multiple matches:
>>>>
>>>> f[ooo][ooo][o]d
>>>>
>>>>
>>>> --
>>>> Willem van Rumpt
>>>
>>> But according to me means "o{1,3}" that I must have at least 1 o and
>>> maximum
>>> of three o.
>>> I meam what is the purpose of using a max of 3 here then ?
>>>
>>> //Tony
>>>
>>>
>>
>> Hi Tony.
>> You have to specify that the previous/next letter can't be an "o".
>>
>> I am not an expert on regex but this should work
>>
>> [^o]o{1,3}[^o]
>>
>
> I don't want a solution I want to know why this regular expression does not
> work as I expect.
> But according to me means "o{1,3}" that I must have at least 1 o and maximum
> of three o.
> I meam what is the purpose of using a max of 3 here then ?
>
> //Tony
>
>

I just corrected myself before reading this post. Check out the other post. But basically when it
finds a match it start to find the next match. You did not specify that the next character can't be
an "o". You have to be more specific.

So it found the first match and then it starts again to find the next match. Both matches are valid.

Does that make sense.

--
Adhal
From: Alberto Poblacion on
"Tony Johansson" <johansson.andersson(a)telia.com> wrote in message
news:ukK1bfA%23KHA.4316(a)TK2MSFTNGP04.phx.gbl...
> I don't want a solution I want to know why this regular expression does
> not work as I expect.
> But according to me means "o{1,3}" that I must have at least 1 o and
> maximum
> of three o.

No, that's not correct. "o{1,3}" means that "somewhere in the middle of
the string there must be a sequence consisting of at least one and at most
three o's. The rest of the string may be anythig (including more o's)".

> I meam what is the purpose of using a max of 3 here then ?

In this particular instance, the max is useless. But it can be useful
when there are more things in the expression. For instance, "Ao{1,3}B" would
match "AoooB", but it would not match "AooooB".



From: Willem van Rumpt on
Tony Johansson wrote:

>
> But according to me means "o{1,3}" that I must have at least 1 o and maximum
> of three o.
> I meam what is the purpose of using a max of 3 here then ?
>
> //Tony
>

The purpose of three is "find me a sequence of 'o' characters, at least
1, and at most three long". And that's what it does. You never instruct
the regex to stop searching beyond the first match, so it matches
everything it can find. You also don't tell it that the it has to be a
sequence of "o" embedded within non "o"'s (and even then: should
"fooodod" result in two matches?).

I think the regex will look something like

[!^o][o{1,3}][!^o]

but I'm almost totally incompetent with regexes (I usually use an online
reference and regex tester to evaluate what I get). You're best bets for
a really appropriate answer are reading up on the subject, or wait until
a regex guru answers you.

--
Willem van Rumpt
From: Tony Johansson on
"Willem van Rumpt" <nothing(a)nowhere.com> skrev i meddelandet
news:u4jUwtA%23KHA.5464(a)TK2MSFTNGP05.phx.gbl...
> Tony Johansson wrote:
>
>>
>> But according to me means "o{1,3}" that I must have at least 1 o and
>> maximum of three o.
>> I meam what is the purpose of using a max of 3 here then ?
>>
>> //Tony
>
> The purpose of three is "find me a sequence of 'o' characters, at least 1,
> and at most three long". And that's what it does. You never instruct the
> regex to stop searching beyond the first match, so it matches everything
> it can find. You also don't tell it that the it has to be a sequence of
> "o" embedded within non "o"'s (and even then: should "fooodod" result in
> two matches?).
>
> I think the regex will look something like
>
> [!^o][o{1,3}][!^o]
>
> but I'm almost totally incompetent with regexes (I usually use an online
> reference and regex tester to evaluate what I get). You're best bets for a
> really appropriate answer are reading up on the subject, or wait until a
> regex guru answers you.
>
> --
> Willem van Rumpt

As you say The purpose of three is "find me a sequence of 'o' characters,
at least
1, and at most three long"

So can I say that the expression means find at least one 'o' at most three
'o' anywhere in the string.
It could be at the very beginning in the middle or at the very end what the
other character are doesn't matter.

//Tony


From: Jeff Johnson on
"Tony Johansson" <johansson.andersson(a)telia.com> wrote in message
news:uPvbXGA%23KHA.420(a)TK2MSFTNGP02.phx.gbl...

> Here I say minimum 1 o and maximum three o but here I have more then three
> o and this expression give true but
> it should give false according to me ?
>
> bool status = Regex.IsMatch("foooooood", "o{1,3}");

The main problem is that you're not understanding how the {m,n} qualifier
works. Specifically, you're reading too much into its abilities. You
shouldn't think of {m,n} as meaning "AT LEAST m occurrences and AT MOST n
occurrences" but rather "FROM m TO n occurrences." The difference is subtle
but important. The first phrase suggests that there must be some limitation
of the letter "o" in your regex above, which is how you were interpreting
it. The second just says "as long as I can find anywhere from 1 to 3 o's,
I'm happy." And the regex engine can definitely find from 1 to 3 o's. In
fact, it can do it three times: 2 sets of 3 and 1 single o.

In order to do what you thought it would do, your best bet is to use an
advanced regex technique called lookaround. Lookaround allows you to look at
characters before your intended match or after it and require that a pattern
either be present or absent. Requiring a pattern to be present is called
positive lookaround, while requiring it to be absent is negative lookaround.
Actually, you don't usually say "lookaround" but rather you indicate the
direction, so you can have positive or negative lookbehind (searching for a
pattern before your match pattern) and positive and negative lookahead
(searching for a pattern after your match pattern).

To get 3 and only 3 o's, you'd need to both look behind and look ahead to
make sure that there isn't a 4th o either before or after your pattern. The
regex would look like this:

(?<!o)o{1,3}(?!o)

This WILL find matches in the following input:

fod
food
foood
Zoo
to
oops
hotfoot

It will NOT find matches in this input:

foooooood
Zooool
ooooooh! aaaaah!