• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What's the Purpose of the Boundary Matchers Caret and Dollar Sign?

 
Ranch Hand
Posts: 246
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wanted to do some pattern detection using regular expressions, so I went to "http://docs.oracle.com/javase/7/docs/api" and looked up class {String} and scrolled down to the section for method {matches()}, where the documentation said, "Tells whether or not this string matches the given regular expression." I clicked on "regular expression", and that took me to the page for class {Pattern}, down to the section labeled <Summary of regular-expression constructs>. I scrolled even further down to the subsection labeled <Boundary matchers>. The caret was listed as a boundary matcher for "The beginning of a line", and the dollar sign was listed as a boundary matcher for "The end of a line". So I wrote the following code:

I expected the call to {actual.matches( expectedOne)} to return {true}, because it had no dollar sign at the end, so it wasn't anchored on the end. But it returned {false}. The call to {actual.matches( expectedTwo)} did return {true}. What's the use of the two anchors, caret and dollar sign, if regular expressions have to have a ".*" at the end, if you want to match a prefix of the line? I mean, I could have written the code:

without the carets, and it would have had the same effect. So why even have the two anchors?

Kevin S
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String.matches tries to match the entire string - so there are implicit "^" and "$" at the beginning and end. That's the difference between Matcher.matches and Matcher.find.

The caret is important if you want to find "The quick brown fox" only once in "The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog."; otherwise you'll find it twice.
 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not mentioned in the docs explicitly, but String#matches(regex) method by default matches at the beginning and till the end of the string. That means, the anchors - ^ and $ are implicit.
If you try the same example with Matcher#find() method, you'll understand the use of anchors. You will get the expected output with that method.
Try out this simple example:


You'll get the following output:

Pattern
Not Matches


Now try changing the regex from "^Pattern" to "^matcher" and see what output you get. I guess that should make it clear. You can frame more such example, to understand it clearly.

To learn more about regex, you can follow - Regular-Expressions Tutorial
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why can you put the character 'a' in a regular expression? Because sometimes you want to match a String containing the letter 'a'. And sometimes you want to match a String containing an end or a beginning of a line.

Let's say you want to read every character at the beginning of a line. Here's how you could do it:
Note that I haven't tested this, and you also need to switch
 
reply
    Bookmark Topic Watch Topic
  • New Topic