IT Community - Software Programming, Web Development and Technical Support

How to use Regular expression in Java?

This is a discussion on How to use Regular expression in Java? within the Java Programming forums, part of the Software Development category; Can any body tell me how to use Regular expression in java?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > Java Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 10-11-2007, 11:07 PM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Default How to use Regular expression in Java?

Can any body tell me how to use Regular expression in java?
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-15-2007, 07:14 AM
leoraja8 leoraja8 is offline
D-Web Sr.Programmer
 
Join Date: May 2007
Posts: 194
leoraja8 is on a distinguished road
Smile Re: How to use Regular expression in Java?

JDK versions 1.4.0 and later have comprehensive support for regular expressions through the standard java.util.regex package. Because Java lacked a regex package for so long, there are also many 3rd party regex packages available for Java. I will only discuss Sun's regex library that is now part of the JDK. Its quality is excellent, better than most of the 3rd party packages. Unless you need to support older versions of the JDK, the java.util.regex package is the way to go.

Regular expressions are a way to describe a set of strings based on common characteristics shared by each string in the set (ie. by pattern matching). They can be used as a tool to search, edit or manipulate text or data. One common use is validation of data entry strings.

All classes related to regular expressions are found in the java.util.regex package which must be imported.

Java regular expression patterns use a syntax similar to the one used by perl. The best reference is found at sun.com.

Simple examples of the use of regular expressions are:

Code:
Pattern p = Pattern.compile("a*b");
 Matcher m = p.matcher("aaaaab");
 boolean b = m.matches();
As a convenience for a one-time use situation the matches method simplifies the syntax (but does not precompile the pattern).

Code:
boolean b = Pattern.matches("a*b", "aaaaab");
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 11-20-2007, 12:52 AM
krishnakumar krishnakumar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 206
krishnakumar is on a distinguished road
Cool Re: How to use Regular expression in Java?

The article is a little too short. Actually If I want to match any '\'? Should I write "\\" or "\\\" or "\\\\"?
__________________
Krishnakumar.S
Beware of Everything -that is un true; stick to the Truth shall succeed slowly but steadily
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 11-28-2007, 08:45 PM
leoraja8 leoraja8 is offline
D-Web Sr.Programmer
 
Join Date: May 2007
Posts: 194
leoraja8 is on a distinguished road
Cool Re: How to use Regular expression in Java?

Hmm….

If you want to match '\' then you need to use '\\\\'
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
what is regular expression? saravanan PHP Programming 8 03-28-2008 06:12 AM
Regular expression tips & tricks prasath Database Support 8 09-24-2007 09:55 PM
Regular expression Sathish Kumar C# Programming 23 09-10-2007 06:00 AM
Can any one give regular expression for this string? raj PHP Programming 1 07-24-2007 11:42 PM
Regular expression vadivelanvaidyanathan Perl 0 07-15-2007 06:21 PM


All times are GMT -7. The time now is 04:39 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0