java compiler errors on regexs

K

kramer31

Hi.

I have an array of strings that I'm trying to use in regexs. the
following line is generating Several errors:

BrowserIdentification.java:17: illegal escape character
protected static String[] acceptedUserAgentPatterns = {"MSIE\s?(\d)+
\.", "Firefox/(\d)\."};


It looks like it is complaining about the \d (which I read about in
the java regex documentation) as well as the \. (which I have to
escape so that it only matches a literal '.', right?

BTW, I am using the ( ) so that I can use matcher.group, later. I'm
compiling using 1.4.2 specifically jrockit-R27.1.0-jdk1.4.2_12

Am I just missing something really obvious here?
 
A

Arne Vajhøj

kramer31 said:
I have an array of strings that I'm trying to use in regexs. the
following line is generating Several errors:

BrowserIdentification.java:17: illegal escape character
protected static String[] acceptedUserAgentPatterns = {"MSIE\s?(\d)+
\.", "Firefox/(\d)\."};


It looks like it is complaining about the \d (which I read about in
the java regex documentation) as well as the \. (which I have to
escape so that it only matches a literal '.', right?

BTW, I am using the ( ) so that I can use matcher.group, later. I'm
compiling using 1.4.2 specifically jrockit-R27.1.0-jdk1.4.2_12

Am I just missing something really obvious here?

\ is also escape in Java strings, so you need to double them
from \ to \\.

Arne
 
K

Knute Johnson

kramer31 said:
Hi.

I have an array of strings that I'm trying to use in regexs. the
following line is generating Several errors:

BrowserIdentification.java:17: illegal escape character
protected static String[] acceptedUserAgentPatterns = {"MSIE\s?(\d)+
\.", "Firefox/(\d)\."};


It looks like it is complaining about the \d (which I read about in
the java regex documentation) as well as the \. (which I have to
escape so that it only matches a literal '.', right?

BTW, I am using the ( ) so that I can use matcher.group, later. I'm
compiling using 1.4.2 specifically jrockit-R27.1.0-jdk1.4.2_12

Am I just missing something really obvious here?

Yes but don't feel bad we all had this problem at one point.

You need to use two backslashes in Java because of the way the string is
interpreted. So to match a group of numbers use \\d+ or the literal .
use \\.

Get yourself a modern compiler, even JRockit has a newer version.
 
K

kramer31

Thanks for the help.

BTW I have many newer versions of Java, but this pertains to an old
legacy app.


kramer31 said:
I have an array of strings that I'm trying to use in regexs. the
following line is generating Several errors:
BrowserIdentification.java:17: illegal escape character
protected static String[] acceptedUserAgentPatterns = {"MSIE\s?(\d)+
\.", "Firefox/(\d)\."};
It looks like it is complaining about the \d (which I read about in
the java regex documentation) as well as the \. (which I have to
escape so that it only matches a literal '.', right?
BTW, I am using the ( ) so that I can use matcher.group, later. I'm
compiling using 1.4.2 specifically jrockit-R27.1.0-jdk1.4.2_12
Am I just missing something really obvious here?

Yes but don't feel bad we all had this problem at one point.

You need to use two backslashes in Java because of the way the string is
interpreted. So to match a group of numbers use \\d+ or the literal .
use \\.

Get yourself a modern compiler, even JRockit has a newer version.
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top