>> Hi, in this regex I have two different results but why?
>>
[quoted text clipped - 13 lines]
>
> df.
Additionally, if you wonder why your second result is "false" (instead
of wondering why the first one is "true"), you might want to learn about
quantifiers.
Lots of stuff about regexes online, for instance:
http://www.regular-expressions.info/
df.
josh - 20 Jun 2007 13:57 GMT
> >> Hi, in this regex I have two different results but why?
>
[quoted text clipped - 20 lines]
>
> df.
the pattern [a-z&&[def]] should mean:
a char between 'a' and 'z' AND a char 'd' or 'e' or 'f'. But the test
fails!
Now my question: are the nested squares and logical && regex standard?
If I
put in RegexBuddy that pattern [a-z&&[def]] the last square is treated
as a single char!
Daniele Futtorovic - 20 Jun 2007 14:50 GMT
>>>> Hi, in this regex I have two different results but why?
>>>> Pattern p = Pattern.compile("[a-z&&[def]]");
[quoted text clipped - 15 lines]
> the pattern [a-z&&[def]] should mean:
> a char between 'a' and 'z' AND a char 'd' or 'e' or 'f'.
As a matter of fact, it means "'d', 'e' or 'f'".
Sorry, I didn't took notice of the intersection at first, as I focussed
on the absence of any quantifier. My second comment was therefore wrong;
the results you get are perfectly what one should expect.
Again: mind the difference between /find/ and /match/. You regex can
capture but one character. So it won't /match/ a four-character-long
string. You will however be able to /find/ the pattern in the input.
> But the test
> fails!
Tell us what you'd expect and why.
> Now my question: are the nested squares and logical && regex standard?
> If I
I don't quite remember whether they're part of the POSIX standard but
they definitely are supported in the java.util.regex package.
> put in RegexBuddy that pattern [a-z&&[def]] the last square is treated
> as a single char!
Don't know 'bout that.
> > Hi, in this regex I have two different results but why?
>
[quoted text clipped - 13 lines]
>
> df.
Ok I read carefully and I understand that matches() find a complete
sequence while
find() a sub-sequence.
But:
boolean b2 = Pattern.matches("[a-z&&[def]]", "ad");
give me false! May be it does not mean find:
a char between 'a' and 'z' AND a char 'd' or 'e' or 'f' ???'
where is my conceptual error???
Joshua Cranmer - 20 Jun 2007 14:42 GMT
> But:
> boolean b2 = Pattern.matches("[a-z&&[def]]", "ad");
>
> give me false! May be it does not mean find: a char between 'a' and 'z'
> AND a char 'd' or 'e' or 'f' ???' where is my conceptual error???
From the Javadocs for Pattern:
[a-z&&[def]] d, e, or f (intersection)
What you're trying to say is the regex [a-z][def]. For more information,
go see http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html.
There's a lot of information on regex there, and even more if you search
for Perl regex.
josh - 20 Jun 2007 15:04 GMT
> > But:
> > boolean b2 = Pattern.matches("[a-z&&[def]]", "ad");
[quoted text clipped - 10 lines]
> There's a lot of information on regex there, and even more if you search
> for Perl regex.
Does have this intersection the same meaning of the mathematic set
intersection concept?
If yes this code is also false:
boolean b2 = Pattern.matches("[a-z&&[def]]", "de"); // false
but d of first a-z and d of def are intersected or NOT!
Please give me an example that returns true!
kcwong - 20 Jun 2007 15:11 GMT
> Does have this intersection the same meaning of the mathematic set
> intersection concept?
[quoted text clipped - 3 lines]
>
> Please give me an example that returns true!
The regular expression means the intersection between "any one from a
to z" and "d, e or f"... which is "d, e, or f".
And then you match a string to that result.
If you look carefully, the regular expression has only one single
class with no quantifiers. So it will never match "de", which has two
characters.
Roedy Green - 29 Jun 2007 16:15 GMT
On Wed, 20 Jun 2007 13:42:05 GMT, Joshua Cranmer
<Pidgeot18@verizon.net> wrote, quoted or indirectly quoted someone who
said :
>[a-z&&[def]] d, e, or f (intersection)
the way would normally use && intersection is like this:
[a-z]&&[^m-p]]
a-z but not m-p
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
kcwong - 20 Jun 2007 14:48 GMT
> But:
> boolean b2 = Pattern.matches("[a-z&&[def]]", "ad");
>
> give me false! May be it does not mean find:
> a char between 'a' and 'z' AND a char 'd' or 'e' or 'f' ???'
> where is my conceptual error???
Let's take it apart.
[a-z&&[def]]
The inner class, [def] means either d, e, or f will match.
The first part of the outer class, a-z means a to z will match.
Combining first part with second part with &&, which means
INTERSECTION.
The Java API doc of the Pattern class gave you exactly the above
example:
[a-z&&[def]] d, e, or f (intersection)
What you're looking for is probably [a-z][def].
Roedy Green - 29 Jun 2007 16:13 GMT
>boolean b2 = Pattern.matches("[a-z&&[def]]", "ad");
>
>give me false! May be it does not mean find:
>a char between 'a' and 'z' AND a char 'd' or 'e' or 'f' ???'
>where is my conceptual error???
What SINGLE character is between a - z and also in the list d e f?
There are only three possibilities that fit the bill d e and f
"and" does not mean "and is followed by" it means "and also is"
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com