• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

a problem about regular expression  RSS feed

 
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In java there are are many methods to parse the regular expressions
for example if we choose replace method provided my String class then we can just go thru this example program for rplacing the expression




this program illustrates about replacing the characters in a regular expression


so my doubt is why and when i should prefer for Pattern and matcher and and when i shoudl go for replace method of String class???


and why java provide these Matcher an Patter classes while we already have String class for replacing the characters :roll:


can any one provide me some information on this issue


thanks in advance



regards

saikrishna
 
author
Posts: 23832
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

so my doubt is why and when i should prefer for Pattern and matcher and and when i shoudl go for replace method of String class???



Regular expression is more than just replacing strings. It is much more powerful than that. I suggest that you take a look at the matcher API for the full details.

But if all you need is to replace parts of a string, then I would recommend using the string class.

and why java provide these Matcher an Patter classes while we already have String class for replacing the characters



It's the other way around, the regex methods of the string class are convenience methods -- they call the pattern/matcher class internally.

Henry
[ October 28, 2006: Message edited by: Henry Wong ]
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.regular-expressions.info/tutorial.html

Also note that the replace(CharSequence, CharSequence) method was only added in JDK 1.5. I suspect it was added because of all the people complaining about having to deal with regexes (or who didn't realize they were dealing with regexes) when all they wanted to do was replace a simple substring.
 
That which doesn't kill us makes us stronger. I think a piece of pie wouldn't kill me. Tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!