Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

regular expressions

Thread view: 
TobiMc3@gmail.com - 06 Aug 2007 15:47 GMT
This is was part of a homework problem (I know how everyone loves
those :-) ) but now the homework is turned in, and I am left with a
nagging question.

I was trying to find a good resource for regular expressions on the
web-but couldn't really find anything that was cut and paste.

In any event, was wondering if anyone had an opinion on a good regex
tutorial.  I have recently purchased "mastering regular expressions"
from O'Reilly-but haven't had an opportunity to begin reading it in
earnest.

The problem in question:

Attempting to use a regex pattern to verify if a String has two alpha
characters and two numeric characters.  I manged to do other more
basic things, but I couldn't figure that one out. The statement I had
was evaluating to true in the event that the two numbers or letters
were group together-however obviously that would fail if the string
was X1T5-a pattern where the digits or numbers were not right next to
each other.

Thanks for any input/wisdom.
rhaazy - 06 Aug 2007 16:10 GMT
this seems like more of a DFA problem
Patricia Shanahan - 06 Aug 2007 16:19 GMT
> This is was part of a homework problem (I know how everyone loves
> those :-) ) but now the homework is turned in, and I am left with a
[quoted text clipped - 19 lines]
>
> Thanks for any input/wisdom.

Did the homework require use of a regular expression?

Patricia
TobiMc3@gmail.com - 06 Aug 2007 16:21 GMT
No, it didn't.  I was trying to expand my horizons.
TobiMc3@gmail.com - 06 Aug 2007 16:34 GMT
On Aug 6, 11:21 am, "Tobi...@gmail.com" <Tobi...@gmail.com> wrote:
> No, it didn't.  I was trying to expand my horizons.

If nothing else, I thought I could break it up.  First test that it
contained two letters, then test that it contained two numbers.
Patricia Shanahan - 06 Aug 2007 16:55 GMT
> On Aug 6, 11:21 am, "Tobi...@gmail.com" <Tobi...@gmail.com> wrote:
>> No, it didn't.  I was trying to expand my horizons.
>
> If nothing else, I thought I could break it up.  First test that it
> contained two letters, then test that it contained two numbers.

I would probably use a single scan approach, with two counters, one for
letters and one for numbers.

Patricia
bugbear - 06 Aug 2007 17:37 GMT
>> On Aug 6, 11:21 am, "Tobi...@gmail.com" <Tobi...@gmail.com> wrote:
>>> No, it didn't.  I was trying to expand my horizons.
[quoted text clipped - 4 lines]
> I would probably use a single scan approach, with two counters, one for
> letters and one for numbers.

If you want to be regex obsessed, you could...

if(/\d.*\d/ && /[a-zA-Z].*[a-zA-Z]/ && /^....$/)

(apols for typos etc)

  BugBear
Oliver Wong - 06 Aug 2007 22:55 GMT
(original message didn't arrive here, which is why I'm replying to
Patricia's post.)

>> This is was part of a homework problem (I know how everyone loves
>> those :-) ) but now the homework is turned in, and I am left with a
[quoted text clipped - 7 lines]
>> from O'Reilly-but haven't had an opportunity to begin reading it in
>> earnest.

   I don't think this is what you're looking for, but for what it's
worth, the way I learned Regular Expressions was from reading Michael
Sipser's Theory of Computation book
(http://www.amazon.com/Introduction-Theory-Computation-Michael-Sipser/dp/053494728X)

   Note that he teaches "pure" regular expression, which doesn't contain
a lot of the fancy features which Perl or Java's regular expression have
(such as back-references).

   Additionally, the book isn't intended to teach regular expressions in
particular, but rather covers it as part of the journey to learning theory
of computation in general (e.g. what is or is not possible to compute on a
computer, etc.)

>> The problem in question:
>>
[quoted text clipped - 5 lines]
>> was X1T5-a pattern where the digits or numbers were not right next to
>> each other.

   The method I use is to draw whatever it is I want to verify as a
finite state machine (representing your condition as a state machine was
pretty straight forward, requiring only 5 states: [0 alpha 0 num], [1
alpha 0 num], [0 alpha 1 num], [1 alpha 1 num], [accepted]), and then
doing a simple, mechanical process to convert from the state diagram into
a regular expression (which is explained in the book).

   The solution I have is quite long and messy, but it works. I'm not
sure if you want me to just post the solution I have, but otherwise I'm
not sure what kind of "hints" I could give, other than the implicit hint
above being the names of my 5 states.

   - Oliver
fireflyc - 07 Aug 2007 01:29 GMT
> (original message didn't arrive here, which is why I'm replying to
> Patricia's post.)
[quoted text clipped - 49 lines]
>
>     - Oliver

i think you need a small book.
a tao of regular.
you can google it.
Stefan Ram - 06 Aug 2007 16:34 GMT
>Attempting to use a regex pattern to verify if a String has two
>alpha characters and two numeric characters.

public class Main
{ public static void test( final java.lang.String text )
 { java.lang.System.out.println
   ( text + " " + java.util.regex.Pattern.matches
     ( "(?=(?:[^a-z]*[a-z]){2})(?=(?:[^0-9]*[0-9]){2}).*", text )); }

 public static void main( final java.lang.String[] args )
 { test( "=====" );
   test( "=a===" );
   test( "=a=b=" );
   test( "=a1b=" );
   test( "=a1b2" );
   test( "=a1=b2" );
   test( "=a1=b=2" );
   test( "=a1b2d" );
   test( "=a1b2d3" );
   test( "=a1b2d3" );
   test( "=a1=2" );
   test( "==1=2" );
   test( "==1=2" ); }}

===== false
=a=== false
=a=b= false
=a1b= false
=a1b2 true
=a1=b2 true
=a1=b=2 true
=a1b2d true
=a1b2d3 true
=a1b2d3 true
=a1=2 false
==1=2 false
==1=2 false


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.