my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Difference between regular expressions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference between regular expressions "[0-8&&[4-6]] and [4-6]"" Watch "Difference between regular expressions "[0-8&&[4-6]] and [4-6]"" New topic
Author

Difference between regular expressions "[0-8&&[4-6]] and [4-6]"

Raman Ghai
Ranch Hand

Joined: Jan 01, 2012
Posts: 51
Both of these seem the same to me . then , What is the use of && in regular expressions ?
Check the following test cases :

Another one :
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
I think you might do well to read a regex tutorial; there are many, but I like this one in the Java Tutorials. Try the “character classes” section. I think you are correct that the two regexes are equivalent to each other.
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1057
    
  10

Are you implying that the && has no use? Limited use maybe (I have only used it a few of times in over 20 years of using regex) but it does have uses. I can only remember using it in a negative context when wanting to exclude characters from a character set. For example -

"[\p{Alpha}&&[^Pt]]"

This will create an alpha set excluding P and t.

Raman Ghai
Ranch Hand

Joined: Jan 01, 2012
Posts: 51
@Richard Tookey yeah , it could be used to subtract characters from a particular range , that's what I thought too.
@Campbell Ritchie thanks for the suggestion
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
You’re welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between regular expressions "[0-8&&[4-6]] and [4-6]"