Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

regex

Thread view: 
josh - 20 Jun 2007 11:45 GMT
Hi, in this regex I have two different results but why?

Pattern p = Pattern.compile("[a-z&&[def]]");
Matcher m = p.matcher("adef");

boolean b = m.find(); // true
boolean b2 = Pattern.matches("[a-z&&[def]]", "adef"); // false

If I try to use another regex tool as RegexBuddy I also have false

But the is the intersection a regex standard feature?

Thanks
Daniele Futtorovic - 20 Jun 2007 13:25 GMT
> Hi, in this regex I have two different results but why?
>
[quoted text clipped - 9 lines]
>
> Thanks

Mind the difference between /find/ and /match/.

df.
Daniele Futtorovic - 20 Jun 2007 13:28 GMT
>> 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.
josh - 20 Jun 2007 14:34 GMT
> > 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
Roedy Green - 29 Jun 2007 15:41 GMT
>Pattern p = Pattern.compile("[a-z&&[def]]");
>Matcher m = p.matcher("adef");
[quoted text clipped - 5 lines]
>
>But the is the intersection a regex standard feature?

Here is your code in runnable form:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Intersection
  {

  /**
   * test harness
   *
   * @param args not used
   */
  public static void main ( String[] args )
     {
     Pattern p = Pattern.compile("[a-z&&[def]]");
     Matcher m = p.matcher("adef");
     boolean b = m.find(); // true
     System.out.println(b);
     boolean b2 = Pattern.matches("[a-z&&[def]]", "adef"); // false
     System.out.println(b2);

     }
  }

find is true because  the letter d exists in the string adef.  The
pattern searches for any of  "def'.

matches is false because 'adef' does not match the pattern.  Patterns
must be a single character long and be a def.

See http://mindprod.com/jgloss/regex.html#MATCHINGANDFINDING

--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.