This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regular Expression issue

 
buntha Choudhary
Ranch Hand
Posts: 136
Android Eclipse IDE Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to write a regular expression for a scenario where -
user can enter a string containing only a-z or A-Z and may end with a single .

The following are Words :
"ab", "ab."

The following are not Words :
"ab..", "a.b", ".ab", "a.b.", "a2b.", "."

I wrote^([a-zA-Z]\\w(\\D+|\\.))$ but it is failing with the scenario of . in the mid.
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have you got \\w and \\D in that regular expression?
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about counting the letters?
 
buntha Choudhary
Ranch Hand
Posts: 136
Android Eclipse IDE Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am very new to Regular Expression.

From the scenario mentioned, there should not be any digit , so I appened \D.
Counting of letters is not required.
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And \\D will find the . which isn't a digit.

Try reading this.
 
buntha Choudhary
Ranch Hand
Posts: 136
Android Eclipse IDE Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I got the flaw.
But I've some doubts-

A dot(.) can be provided in the end of the String but not in the mid. So I needed to provide some check for that . How can I do that.
A digit is not comfortable anywhere in the String, so how should be my pattern.
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
buntha Choudhary wrote:Oh, I got the flaw.
But I've some doubts-

A dot(.) can be provided in the end of the String but not in the mid. So I needed to provide some check for that . How can I do that.
A digit is not comfortable anywhere in the String, so how should be my pattern.

1) If you specify the pattern to be a-z or A-Z, it can't be digit anyway.
The occurrence of this pattern exists 1 or more times.

2) 1 dot after that

And that's it.
 
buntha Choudhary
Ranch Hand
Posts: 136
Android Eclipse IDE Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I am not mandating the dot in the end. We can or can not provide the dot in the end.
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
buntha Choudhary wrote:But I am not mandating the dot in the end. We can or can not provide the dot in the end.

Sorry, misread it, the dot with 0 or 1 time.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This shouldn't be too hard:
- [a-zA-Z] one or more times
- a dot zero or one times

Paste these together, add a leading ^ and trailing $, and you have your regex.
 
buntha Choudhary
Ranch Hand
Posts: 136
Android Eclipse IDE Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all.

The pattern I got - ^([a-zA-Z]+\\.{0,1})$
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a simpler way to do it than {0,1}
 
buntha Choudhary
Ranch Hand
Posts: 136
Android Eclipse IDE Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell
Can you please suggest me some more optimistic solution.
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic