java compiler errors on regexs

Discussion in 'Java' started by kramer31, Jul 8, 2008.

  1. kramer31

    kramer31 Guest

    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?
     
    kramer31, Jul 8, 2008
    #1
    1. Advertising

  2. kramer31

    Arne Vajhøj Guest

    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
     
    Arne Vajhøj, Jul 8, 2008
    #2
    1. Advertising

  3. 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
     
    Knute Johnson, Jul 8, 2008
    #3
  4. kramer31

    Roedy Green Guest

    On Tue, 8 Jul 2008 14:53:13 -0700 (PDT), kramer31
    <> 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
     
    Roedy Green, Jul 9, 2008
    #4
  5. kramer31

    kramer31 Guest

    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 <>
    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
     
    kramer31, Jul 11, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Seberino
    Replies:
    1
    Views:
    539
    Hao Lian
    Sep 12, 2009
  2. Chris Seberino
    Replies:
    4
    Views:
    520
    Robert Kern
    Sep 13, 2009
  3. Replies:
    5
    Views:
    114
  4. Matija Papec

    two regexs

    Matija Papec, Oct 4, 2003, in forum: Perl Misc
    Replies:
    14
    Views:
    175
    Matija Papec
    Oct 7, 2003
  5. Replies:
    6
    Views:
    125
    Ben Morrow
    Aug 13, 2004
Loading...

Share This Page