Carets (^) in regular expressions

  • Follow


     The Java SE 6.0 API documentation for the Pattern class injava.util.regex says the caret ("^") can be used in a regularexpression to find the beginning of a line.  However, it can also beused to denote negation (see "Character classes" section).  How doesJava tell the difference in this usage?Thanks, Alan
0
Reply jalanthomas (125) 10/6/2007 2:27:53 AM

Alan wrote:>      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
0
Reply ISO 10/6/2007 2:35:42 AM


Arne,      Thanks.  This is the first regexp I`ve used.  It turns out myproblem was using "^\p" instead of the correct "\P".Alan
0
Reply Alan 10/6/2007 3:04:59 AM

On Sat, 06 Oct 2007 03:04:59 -0000, Alan <jalanthomas@verizon.net>wrote, quoted or indirectly quoted someone who said :>      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-- Roedy Green Canadian Mind ProductsThe Java Glossaryhttp://mindprod.com
0
Reply Roedy 10/6/2007 3:29:32 AM

Alan wrote:>       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
0
Reply ISO 10/6/2007 3:46:50 AM

4 Replies
114 Views

(page loaded in 1.58 seconds)

Similiar Articles:




7/29/2012 2:41:12 AM


Reply: