File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Raman Ghai
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47216
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Pie
Posts: 1166
17
Java Linux Netbeans IDE
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Pie
Posts: 47216
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic