regex

J

josh

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
 
D

Daniele Futtorovic

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

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

df.
 
D

Daniele Futtorovic

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

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

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

josh

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
Mind the difference between /find/ and /match/.

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.

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

josh

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?

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

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

Joshua Cranmer

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

kcwong

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].
 
D

Daniele Futtorovic

On 20/06/2007 12:45, josh wrote:
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
Mind the difference between /find/ and /match/.
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.

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

josh

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 seehttp://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.

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

kcwong

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!

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

Roedy Green

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?


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
 
R

Roedy Green

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"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top