java compiler errors on regexs  
Author Message
kramer31





PostPosted: 2008-7-9 5:53:13 Top

java-programmer, java compiler errors on regexs 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?

 
Arne Vajh鴍





PostPosted: 2008-7-9 6:05:00 Top

java-programmer >> java compiler errors on regexs kramer31 wrote:
> 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
 
Roedy Green





PostPosted: 2008-7-9 15:19:00 Top

java-programmer >> java compiler errors on regexs On Tue, 8 Jul 2008 14:53:13 -0700 (PDT), kramer31
<kramer.newsreader@gmail.com> wrote, quoted or indirectly quoted
someone who said :

>protected static String[] acceptedUserAgentPatterns = {"MSIE\s?(\d)+
>\.", "Firefox/(\d)\."};

the problem is \ is a magic character both to Java string literals and
to Regex. see
http://mindprod.com/jgloss/regex.html#QUOTING
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
 
kramer31





PostPosted: 2008-7-11 7:31:00 Top

java-programmer >> java compiler errors on regexs Thanks for the help.

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


On Jul 8, 3:18 pm, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:
> kramer31 wrote:
> > 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.
>
> --
>
> Knute Johnson
> email s/nospam/knute2008/
>
> --
> Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
> ------->>>>>>http://www.NewsDemon.com<<<<<<------
> Unlimited Access, Anonymous Accounts, Uncensored Broadband Access