regular expressions

T

TobiMc3

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.
 
P

Patricia Shanahan

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.

Did the homework require use of a regular expression?

Patricia
 
T

TobiMc3

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.
 
S

Stefan Ram

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
 
P

Patricia Shanahan

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
 
B

bugbear

Patricia said:
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
 
O

Oliver Wong

(original message didn't arrive here, which is why I'm replying to
Patricia's post.)

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 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
 
F

fireflyc

(original message didn't arrive here, which is why I'm replying to
Patricia's post.)

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/...)

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 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

i think you need a small book.
a tao of regular.
you can google it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top