GeeCON Prague 2014*
The moose likes Java in General and the fly likes Regarding regular expression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Regarding regular expression" Watch "Regarding regular expression" New topic
Author

Regarding regular expression

santhosh kumar vk
Ranch Hand

Joined: Aug 25, 2009
Posts: 146
Hi,

I want to do the validation using regular expression, It should allow only digit and Asterisk but the asterisk should come in end of the text only
for example 1234* , 123456*, 12345678* it should not come between the digit, Could you please tell me the regular expression for this.


Thanks,
Santhosh Kumar V.K
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3602
    
  15
What have you tried ? In what way did it not work ?

Joanne
santhosh kumar vk
Ranch Hand

Joined: Aug 25, 2009
Posts: 146
Asterisk should allow end of the text only in middle like
1234*
123456*
12345678*

Asterisk should now allow in middle of the text

12*34
1234*5
12345*678

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3602
    
  15
santhosh kumar vk wrote:Asterisk should allow end of the text only in middle like
1234*
123456*
12345678*

Asterisk should now allow in middle of the text

12*34
1234*5
12345*678


Okay. But what regular expressions have you tried ? You will learn a lot more by experimenting than just being given the answer.
santhosh kumar vk
Ranch Hand

Joined: Aug 25, 2009
Posts: 146
I tried "^[0-9*]" regular expression but it allowing asterisk in middle of the text
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3602
    
  15
santhosh kumar vk wrote:I tried "^[0-9*]" regular expression but it allowing asterisk in middle of the text

So did you try separating [0-9*] out into two tokens ?
santhosh kumar vk
Ranch Hand

Joined: Aug 25, 2009
Posts: 146
could you please send me the regular expression for this?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3602
    
  15
santhosh kumar vk wrote:could you please send me the regular expression for this?

No. The purpose of the ranch is to help people to learn, not give them answers.
If you don't know enough about regular expressions and can't be bothered to read one of the many available tutorials on the web, you could solve this using a couple of methods from the String, Integer/Long and/or Character classes.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4571
    
    5

santhosh kumar vk wrote:could you please send me the regular expression for this?


No, but I'll point you to two learning resources that have helped me whenever I need a regex.
http://docs.oracle.com/javase/tutorial/essential/regex/index.html
http://www.regular-expressions.info/


luck, db
There are no new questions, but there may be new answers.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11351
    
  16

Try explaining what your pattern should be, not what it should or shouldn't allow. for example, if i wanted to find lines that have my name in the middle somewhere, i might say

find lines that have

the beginning of the line, followed by
one or more characters, followed by
a space, followed by
the literal "Fred", followed by
a space, followed by
one or more characters.

this would match lines like
I know a guy named Fred who is a father.
This guy Fred is a little strange

but it would not match
Fred has a daughter
The moderator of Meaningless Drivel is Fred.

once I have that, writing the regex gets MUCH easier.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
GeeCON Prague 2014
 
subject: Regarding regular expression