This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Regex problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Regex problem" Watch "Regex problem" New topic
Author

Regex problem

S Ali
Ranch Hand

Joined: Aug 23, 2009
Posts: 129
Hi all,
I have a problem with regex as I have a user who is supposed to input a number of characters which are a combination of '.' and '*'. To start I don't know how to use the '.' as a character . My string is

String regex = ".*" ;

which of course is translated into zero or many any possible character. I hope you ranchers could help me with this as I'm googling for answers right now and I can find relevant articles.


SCJP 6
S Ali
Ranch Hand

Joined: Aug 23, 2009
Posts: 129
Ok guys I found out that to use . or * as ordinary literals you have to put them between square brackets [.][*] like that,
now I need to find out how to make the user enter a line of 5 characters that is formed from a combination of . and *
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
A better way is to escape those characters with a backslash: \. (or "\\." if it's part of a Java string).


Ping & DNS - my free Android networking tools app
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
S Ali wrote:now I need to find out how to make the user enter a line of 5 characters that is formed from a combination of . and *

pattern is: [\.\*]{5}
Here you find regex basics:
http://java.sun.com/docs/books/tutorial/essential/regex/index.html
After you will read the above tutorial from cover to cover, visit this site for more advanced regex stuff:
http://www.regular-expressions.info/
S Ali
Ranch Hand

Joined: Aug 23, 2009
Posts: 129
Ireneusz Kordal wrote:
S Ali wrote:now I need to find out how to make the user enter a line of 5 characters that is formed from a combination of . and *

pattern is: [\.\*]{5}
Here you find regex basics:
http://java.sun.com/docs/books/tutorial/essential/regex/index.html
After you will read the above tutorial from cover to cover, visit this site for more advanced regex stuff:
http://www.regular-expressions.info/


Many thanks .
Carey Brown
Ranch Hand

Joined: Nov 19, 2001
Posts: 174

Check me on this, but I don't think you need to escape the characters when they appear inside brackets. E.g. "[.*]{5}" should work.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38472
    
  23
I think you are probably correct, Carey Brown, but a look at those tutorials will confirm it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Regex problem