aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regex - Difference in ^[abc] and [^abc] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regex - Difference in ^[abc] and [^abc]" Watch "Regex - Difference in ^[abc] and [^abc]" New topic
Author

Regex - Difference in ^[abc] and [^abc]

O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

I am trying to find out what is the difference between ^[abc] and [^abc]

I tried two simple examples using



Output






:Output


I know that the first one is saying get any character that is not a, b or c

- When you say [^abc], shouldnt abc have been in brackets for the ^ character apply to all three characters?
- Please explain the difference in output of the above two examples.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4658
    
    5

^ outside a character class isn't a negation, it's the start of input.

Recommended reading:
http://download.oracle.com/javase/tutorial/essential/regex/index.html
http://www.regular-expressions.info/

luck, db
There are no new questions, but there may be new answers.
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

oh ok that makes sense.
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Darryl Burke wrote:^ outside a character class isn't a negation, it's the start of input.

Recommended reading:
http://download.oracle.com/javase/tutorial/essential/regex/index.html
http://www.regular-expressions.info/


Thanks for this. That second one is quite good..
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
off the top of my head, I think this topic is outside the scope of the exam... it would be worth double checking

to be sure, the exam focuses on a small subset of regex


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Regex - Difference in ^[abc] and [^abc]