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 / October 2007

Tip: Looking for answers? Try searching our database.

Carets (^) in regular expressions

Thread view: 
Alan - 06 Oct 2007 03:27 GMT
The Java SE 6.0 API documentation for the Pattern class in
java.util.regex says the caret ("^") can be used in a regular
expression to find the beginning of a line.  However, it can also be
used to denote negation (see "Character classes" section).  How does
Java tell the difference in this usage?

Thanks, Alan
Arne Vajhøj - 06 Oct 2007 03:35 GMT
>      The Java SE 6.0 API documentation for the Pattern class in
> java.util.regex says the caret ("^") can be used in a regular
> expression to find the beginning of a line.  However, it can also be
> used to denote negation (see "Character classes" section).  How does
> Java tell the difference in this usage?

Java regex does what other regex does: ^ outside [] is start and
^ inside [] is negation.

Arne
Alan - 06 Oct 2007 04:04 GMT
Arne,
     Thanks.  This is the first regexp I`ve used.  It turns out my
problem was using "^\p" instead of the correct "\P".

Alan
Roedy Green - 06 Oct 2007 04:29 GMT
>      Thanks.  This is the first regexp I`ve used.  It turns out my
>problem was using "^\p" instead of the correct "\P".

for other regex hints, see http://mindprod.com/jgloss/regex.html
Signature

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

Arne Vajhøj - 06 Oct 2007 04:46 GMT
>       Thanks.  This is the first regexp I`ve used.  It turns out my
> problem was using "^\p" instead of the correct "\P".

You could also have used ^ just inside [].

Try running:

import java.util.regex.Pattern;

public class R {
    public static void main(String[] args) {
        System.out.println(Pattern.matches("\\p{Digit}+", "123"));
        System.out.println(Pattern.matches("\\p{Digit}+", "ABC"));
        System.out.println(Pattern.matches("\\P{Digit}+", "123"));
        System.out.println(Pattern.matches("\\P{Digit}+", "ABC"));
        System.out.println(Pattern.matches("[^\\p{Digit}]+", "123"));
        System.out.println(Pattern.matches("[^\\p{Digit}]+", "ABC"));
    }
}

Arne


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.