aspose file tools*
The moose likes Java in General and the fly likes Question on probably regular expressions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Question on probably regular expressions" Watch "Question on probably regular expressions" New topic
Author

Question on probably regular expressions

JayaSiji Gopal
Ranch Hand

Joined: Sep 27, 2004
Posts: 303
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.


SCJP 1.4, SCWCD 1.4<br /> <br />Thanks in advance!<br />Jayashree.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
If you are new to regular expressions, you need a tutorial. Nice one here.
Dariusz Kordonski
Ranch Hand

Joined: Jul 11, 2008
Posts: 49
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 ]
Pratap koritala
Ranch Hand

Joined: Sep 27, 2006
Posts: 252
Recall automata theory on how to recognize strings
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
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

Joined: Feb 25, 2006
Posts: 266
Also answered here:
http://forums.sun.com/thread.jspa?threadID=5317571
Pratap koritala
Ranch Hand

Joined: Sep 27, 2006
Posts: 252
Regular expression is the best for this,though

this also works



[ July 25, 2008: Message edited by: Pratap koritala ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Pratap koritala
Ranch Hand

Joined: Sep 27, 2006
Posts: 252
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

Joined: Jul 11, 2001
Posts: 14112
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...
Pratap koritala
Ranch Hand

Joined: Sep 27, 2006
Posts: 252
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

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Nov 08, 2007
Posts: 457
you can also split the string on the comma, check the length of the resulting array, and the length of each string


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on probably regular expressions