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

Regex problem

 
S Ali
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
S Ali
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A better way is to escape those characters with a backslash: \. (or "\\." if it's part of a Java string).
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1459
16
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are probably correct, Carey Brown, but a look at those tutorials will confirm it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic