Problems with regular expressions using JDK java.util.regex package

T

Tom Maki

Hi,

I have to admit I have never been good at regular expressions and I
have a piece of code that I want to write using the java.util.regex
package. It is quite simple -- it is for a compression filter. Once
thing that is a bit different is that I want to compress all web pages
that don't have the subsequence 'admin' in them (these pages all go
over a LAN so compression would be counterproductive. I can't just
invert the find () boolean since the client will be changing the reg.
exp. over time. Anyway, here is the test case I have been using. You
notice is fails on assertFalse(pattern.matcher(adminUri).find())
statement:

import java.util.regex.Pattern;
import junit.framework.TestCase;

public class RegularExpressionTest extends TestCase {

public void testRexEx() {
String adminUri = "/rss/admin_product_types_start.do";
String reportUri = "/rss/price_change_report_start.do";

String regEx = "[^a][^d][^m][^i][^n]";
Pattern pattern = Pattern.compile(regEx);
assertTrue(pattern.matcher(reportUri).find());
assertFalse(pattern.matcher(adminUri).find());

}
}

Any help would be greatly appreciated!

Thanks,
Tom Maki
 
P

Paul Lutus

Tom said:
Hi,

I have to admit I have never been good at regular expressions and I
have a piece of code that I want to write using the java.util.regex
package. It is quite simple -- it is for a compression filter. Once
thing that is a bit different is that I want to compress all web pages
that don't have the subsequence 'admin' in them (these pages all go
over a LAN so compression would be counterproductive. I can't just
invert the find () boolean since the client will be changing the reg.
exp. over time. Anyway, here is the test case I have been using. You
notice is fails on assertFalse(pattern.matcher(adminUri).find())
statement:

How would we notice that? You are not providing a complete, compilable
example.
import java.util.regex.Pattern;
import junit.framework.TestCase;

public class RegularExpressionTest extends TestCase {

public void testRexEx() {
String adminUri = "/rss/admin_product_types_start.do";
String reportUri = "/rss/price_change_report_start.do";

String regEx = "[^a][^d][^m][^i][^n]";
Pattern pattern = Pattern.compile(regEx);
assertTrue(pattern.matcher(reportUri).find());
assertFalse(pattern.matcher(adminUri).find());

}
}

Any help would be greatly appreciated!

Please post a complete, compilable example, and say exactly what you are
doing and how it fails.
 
O

Oscar kind

Tom Maki said:
I have to admit I have never been good at regular expressions and I
have a piece of code that I want to write using the java.util.regex
package. It is quite simple -- it is for a compression filter. Once
thing that is a bit different is that I want to compress all web pages
that don't have the subsequence 'admin' in them (these pages all go
over a LAN so compression would be counterproductive. I can't just
invert the find () boolean since the client will be changing the reg.
exp. over time. Anyway, here is the test case I have been using. You
notice is fails on assertFalse(pattern.matcher(adminUri).find())
statement:

import java.util.regex.Pattern;
import junit.framework.TestCase;

public class RegularExpressionTest extends TestCase {

public void testRexEx() {
String adminUri = "/rss/admin_product_types_start.do";
String reportUri = "/rss/price_change_report_start.do";

String regEx = "[^a][^d][^m][^i][^n]";
Pattern pattern = Pattern.compile(regEx);
assertTrue(pattern.matcher(reportUri).find());
assertFalse(pattern.matcher(adminUri).find());

}
}

The assertion on the String adminUrio fails, because there is a substring
in it that doesn't start with an 'a', doesn't have 'd' as second
character, etc. The first 5 characters match.

Why not try to match "admin" (or "[Aa][Dd][Mm][Ii][Nn]" for
case-insensitivity)? If it doesn't match, compress. You can even use the
boolean inversion operator (!) for this...


Sorry for the sarcasm, but the first thing I was told about regular
expressions was to make sure what to match; especially as complicated
regular expressions are often WORN (written: once, read: never). Or at
least they are as I write them...
 
T

Tom Maki

Hi Paul,

All that is missing for compilation is the the junit.jar. Do you want
me to post that? Probably easier to download from sourceforge:
http://prdownloads.sourceforge.net/junit/junit3.8.1.zip?download

Thanks for your comments.

Tom

Paul Lutus said:
Tom said:
Hi,

I have to admit I have never been good at regular expressions and I
have a piece of code that I want to write using the java.util.regex
package. It is quite simple -- it is for a compression filter. Once
thing that is a bit different is that I want to compress all web pages
that don't have the subsequence 'admin' in them (these pages all go
over a LAN so compression would be counterproductive. I can't just
invert the find () boolean since the client will be changing the reg.
exp. over time. Anyway, here is the test case I have been using. You
notice is fails on assertFalse(pattern.matcher(adminUri).find())
statement:

How would we notice that? You are not providing a complete, compilable
example.
import java.util.regex.Pattern;
import junit.framework.TestCase;

public class RegularExpressionTest extends TestCase {

public void testRexEx() {
String adminUri = "/rss/admin_product_types_start.do";
String reportUri = "/rss/price_change_report_start.do";

String regEx = "[^a][^d][^m][^i][^n]";
Pattern pattern = Pattern.compile(regEx);
assertTrue(pattern.matcher(reportUri).find());
assertFalse(pattern.matcher(adminUri).find());

}
}

Any help would be greatly appreciated!

Please post a complete, compilable example, and say exactly what you are
doing and how it fails.
 
T

Tom Maki

Thanks Oscar, doing a ! on pattern.matcher(adminUri).find() was my
first thought. I guess I will just have to make it clear to the client
that all web pages will be compressed except for those that match the
regular expression given.

Thanks again,
Tom



Oscar kind said:
Tom Maki said:
I have to admit I have never been good at regular expressions and I
have a piece of code that I want to write using the java.util.regex
package. It is quite simple -- it is for a compression filter. Once
thing that is a bit different is that I want to compress all web pages
that don't have the subsequence 'admin' in them (these pages all go
over a LAN so compression would be counterproductive. I can't just
invert the find () boolean since the client will be changing the reg.
exp. over time. Anyway, here is the test case I have been using. You
notice is fails on assertFalse(pattern.matcher(adminUri).find())
statement:

import java.util.regex.Pattern;
import junit.framework.TestCase;

public class RegularExpressionTest extends TestCase {

public void testRexEx() {
String adminUri = "/rss/admin_product_types_start.do";
String reportUri = "/rss/price_change_report_start.do";

String regEx = "[^a][^d][^m][^i][^n]";
Pattern pattern = Pattern.compile(regEx);
assertTrue(pattern.matcher(reportUri).find());
assertFalse(pattern.matcher(adminUri).find());

}
}

The assertion on the String adminUrio fails, because there is a substring
in it that doesn't start with an 'a', doesn't have 'd' as second
character, etc. The first 5 characters match.

Why not try to match "admin" (or "[Aa][Dd][Mm][Ii][Nn]" for
case-insensitivity)? If it doesn't match, compress. You can even use the
boolean inversion operator (!) for this...


Sorry for the sarcasm, but the first thing I was told about regular
expressions was to make sure what to match; especially as complicated
regular expressions are often WORN (written: once, read: never). Or at
least they are as I write them...
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top