• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regex problem

 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 *
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic