• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on probably regular expressions

 
JayaSiji Gopal
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i have a string, "Alberetta, GA", i want to check if the string has
comma, space, two characters and end of line.

i am new to regular expressions. can someone please help me with this? i thought reg exp would be the best solution for this. let me know if any other alternative exists.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49364
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are new to regular expressions, you need a tutorial. Nice one here.
 
Dariusz Kordonski
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is String.contains() method, and Apache Commons offers StringUtils class, which would propably help you as well. But honestly, you'd better start learning regex It helps a lot

Another useful resource (not directly related to Java)
[ July 24, 2008: Message edited by: Dariusz Kordonski ]
 
Ramya Chowdary
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recall automata theory on how to recognize strings
 
Campbell Ritchie
Sheriff
Pie
Posts: 49364
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pratap koritala:
Recall automata theory on how to recognize strings
Please post more details; if JayaSiji Gopal is unfamiliar with regular expressions, he will probably not be familiar with automata. I presume you mean finite-state automata? I still think a regular expression would be easier.
[ July 25, 2008: Message edited by: Campbell Ritchie ]
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also answered here:
http://forums.sun.com/thread.jspa?threadID=5317571
 
Ramya Chowdary
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regular expression is the best for this,though

this also works



[ July 25, 2008: Message edited by: Pratap koritala ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
Please post more details; if JayaSiji Gopal is unfamiliar with regular expressions, he will probably not be familiar with automata. I presume you mean finite-state automata? I still think a regular expression would be easier.


Of course a regular expression implementation will most likely *use* a finite-state automaton internally.
 
Ramya Chowdary
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yea, can't use regular expressions where count is involved...
of course, you can program in java...thats where pushdown automata and context free grammer comes..
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pratap koritala:
Yea, can't use regular expressions where count is involved...


Huh? I have no idea what you are talking about...
 
Ramya Chowdary
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am telling that regualar languages ( string recognized by reg ex) cannot have count.

Have a try writing regular expression for a string containing
equal no's of A's and B's

(A)^n****(B)^n
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I am aware that that's not a regular language.

I was confused because the only count that is mentioned in the original post is the "two characters" - and that's, of course, possible using regex.
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can also split the string on the comma, check the length of the resulting array, and the length of each string
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic