Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-17-2007, 05:46 PM
Member
 
Join Date: Jul 2007
Posts: 26
romina is on a distinguished road
I can't find the right regular expression
Hello, I want a given string to match a certain pattern and I am using the following function
Code:
public boolean matches(String regex) from java.lang.String
The constraints the strings have to satisfy are the following
  • At least 2 letter a-zA-z
  • Zero or more times an apostrophe (')
  • Zero or more times a space
for example James o'Hara is valid string.

The problem is I can't find the right pattern/regular expression.

I tried this: string.matches([a-zA-Z]{2,}[ ']*) but the example didn't work.
Can anyone help me? I am using eclipse and Java5.0
Thanks
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-07-2007, 06:36 AM
Member
 
Join Date: Jul 2007
Posts: 40
barney is on a distinguished road
The regex I used was
Code:
[a-zA-Z]{2,}[a-zA-Z ']* It seemed to work: Ouch this totally 'sux0rz - false (numbers) Ouch this totally 'sux.rz - false (improper character) Ouch this totally 'suxorz - true a - false (not enough beginning letters) a' - false (not enough beginning letters) a ' - false (not enough beginning letters) a b' - false (not enough beginning letters) a'b - false (not enough beginning letters) ab' - true ab ' - true
The reason that you were having problems is because you weren't allowing for letters after a space or after an apostrophe. The regex string you created assumes that there is no input other than a space or an apostrophe after a space or an apostrophe.
Greetings.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
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
regular expression for unicode tharhan Advanced Java 0 04-01-2008 11:53 PM
Regular expression for file path ravian New To Java 3 01-25-2008 09:24 PM
Regular expression with Unions Java Tip Java Tips 0 01-09-2008 01:03 PM
Regular expression with Intersections Java Tip Java Tips 0 01-09-2008 01:03 PM
Boolean Expression ritwik07 New To Java 3 07-11-2007 05:11 AM


All times are GMT +3. The time now is 11:54 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org