This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring Boot in Practice and have Somnath Musib on-line!
See this thread for details.
Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Regex - please explain

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


output:your dad and your Mom.

I am unable to understand the functionality of appendReplacement() and appendTail().I read the API also.but i am not clear.please explain the output step by step.it is confusing for me.
[ November 15, 2006: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically what you need to know is that this is a search and replace. It searches the input string for "The" and replaces it with "your". (The case is not corrected.)

The exact way this works is not all that intuitive. I suggest actually coding this and experimenting with it which is what I did to learn it. (I'm using NetBeans which is very helpful because it catches any compile errors right away and it provides links to the API docs.)

Once you play with it for a while you realize that it's just the way it works and the details are not really all that interesting. If you print out the StringBuffer "s" each time through the while loop you'll see what it's doing.

The following will work:
while(m.find()){
m.appendReplacement(s,"your");
System.out.println(s);
}
m.appendTail(s);
System.out.println(s);

This prints out:
your
your Dad and your
your Dad and your Mom

(I don't know why in many examples (including SCJP-Bates/Sierra) they use a separate boolean variable for the results of m.find().)

-----

My question for the community is:
Are appendReplacement() and appendTail() on the exam?

Because I think studying for the exam is hard enough without studying material that will not be included. I have looked in the Sierra/Bates SCJP book and it never mentions these methods. And in fact says that "you won't have to understand the Matcher methods that facilitate replacing strings in data."

This type of question came up in a Whizlab practice test, which makes me wonder whether this will come up on the real exam! I spent a considerable amount of time with NetBeans making an example like this to see exactly how it works. While I enjoy learning new methods in Java, my goal right now is to pass the exam!

Any help would be appreciated - especially a comment from Bert as to whether this is included in the exam. Is there anyway to know exactly what Classes and what Methods are on the exam?

Thanks
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pandoo,

import java.util.regex.*;

class RegexSmall
{
public static void main(String[] args)
{
Pattern p1 = Pattern.compile("The");
Matcher m1 = p1.matcher("where are The Dad and The Mom");
StringBuffer sb = new StringBuffer();
while (m1.find()) {
m1.appendReplacement(sb, "your");
}
m1.appendTail(sb);
System.out.println(sb.toString());
}
}

Lets modify input string to "where are The dad and The mom"
what does matcher do? matcher finds match for "The"
now it finds the match at two places:

1) At 10th position so it returns true and then it will append the string till first character of the match found which is 'T' to the string buffer so after first iteration what do we have in string buffer "where are ". After this it will append replacement string "your" to the string buffer.
2) Now second iteration will find match at 22nd position so it will return true, enter the loop again append string " Dad and " then append replacement string "your" to the stringbuffer.

Okay now after this no match is found so loop will end and till this point we will have stringbuffer containing "where are your dad and your". But we need the remaining part also, so we call appendTail(StringBuffer)
method to append remaining string to the string buffer giving output "where are your Dad and your Mom".
 
B pandoo
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ranchy and Fountain.my doubt is cleared
 
B pandoo
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ranchy and Fountain.my doubt is cleared
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic