split() in String: asterisk in character class

Discussion in 'Java' started by Michael, Jun 6, 2004.

  1. Michael

    Michael Guest

    Hello!

    I have the following string Id like to split:
    023876:adm;Hansen,Hans-250000*1997

    String regexp="[:;,-*]";
    givess me an error :
    java.util.regex.PatternSyntaxException: Illegal character range near index 5

    String regexp="[:;,-\*]";
    wont even compile.

    How do I express this character class?

    Thanks

    Michael
     
    Michael, Jun 6, 2004
    #1
    1. Advertising

  2. On 6 Jun 2004 11:36:40 -0700, Michael wrote:

    > I have the following string Id like to split:
    > 023876:adm;Hansen,Hans-250000*1997
    >
    > String regexp="[:;,-*]";


    <http://www.physci.org/split.jsp?text=023876%3Aadm%3BHansen%2CHans-250000*1997&delim=%5B%3A%3B%2C-%5C%5C*%5D>

    (?)

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
     
    Andrew Thompson, Jun 6, 2004
    #2
    1. Advertising

  3. On Sun, 06 Jun 2004 19:12:54 GMT, Andrew Thompson wrote:

    > On 6 Jun 2004 11:36:40 -0700, Michael wrote:
    >
    >> I have the following string Id like to split:
    >> 023876:adm;Hansen,Hans-250000*1997
    >>
    >> String regexp="[:;,-*]";

    >
    > <http://www.physci.org/split.jsp?text=023876%3Aadm%3BHansen%2CHans-250000*1997&delim=%5B%3A%3B%2C-%5C%5C*%5D>


    But I suspect *this* is what you are actually trying to do..
    <http://www.physci.org/split.jsp?text=023876%3Aadm%3BHansen%2CHans-250000*1997&delim=%5B%5C%5C*%3B%3A%2C-%5D>

    ....hmmmm.

    Now that I have the RegEx Lab, I'll have to
    learn how to use these funky patterns... ;-)

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
     
    Andrew Thompson, Jun 6, 2004
    #3
  4. Michael wrote:
    > Hello!
    >
    > I have the following string Id like to split:
    > 023876:adm;Hansen,Hans-250000*1997
    >
    > String regexp="[:;,-*]";
    > givess me an error :
    > java.util.regex.PatternSyntaxException: Illegal character range near index 5
    >
    > String regexp="[:;,-\*]";
    > wont even compile.
    >
    > How do I express this character class?
    >
    > Thanks
    >
    > Michael


    O.K. I found the problem:
    Java takes ",-*" this as a range (komma to asterisk) and fails to
    understand. reshuffling ",*-" solves the problem.

    Michael
     
    Michael Preminger, Jun 6, 2004
    #4
  5. Michael

    Roedy Green Guest

    On 6 Jun 2004 11:36:40 -0700, (Michael) wrote or
    quoted :

    >String regexp="[:;,-\*]";
    >wont even compile.
    >
    >How do I express this character class?


    Ask Quoter. , - \ * all have special meaning in regen and thus need
    to be quoted.

    See http://mindprod.com/jgloss/regex.html
    http://mindprod.com/quoter.html
    http://mindprod.com/projects/regexproofreader.html
    http://mindprod.com/jgloss/regexcomposer.html

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Jun 6, 2004
    #5
  6. Michael

    Chris Smith Guest

    Michael Preminger wrote:
    > > String regexp="[:;,-*]";
    > > givess me an error :
    > > java.util.regex.PatternSyntaxException: Illegal character range near index 5
    > >
    > > String regexp="[:;,-\*]";
    > > wont even compile.


    > Java takes ",-*" this as a range (komma to asterisk) and fails to
    > understand. reshuffling ",*-" solves the problem.


    That's rather fragile, though. Try this instead:

    String regexp = "[:;\\,\\-\\*]";

    I've escaped the comma, hyphen, and star to avoid their interpretation
    as special characters. The tricky part is that since '\' is a special
    character to the Java language, it needs to be escaped itself.

    It's important to note that the "\\" resolution happens in the compiler,
    and has nothing to do with regular expressions. For example, if you
    were writing this same regexp in a properties file to be loaded by your
    program, you might write:

    sep.regexp = [:;\,\-\*]

    Hope that makes sense.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Jun 7, 2004
    #6
    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. Carlos Ribeiro
    Replies:
    11
    Views:
    702
    Alex Martelli
    Sep 17, 2004
  2. Sam Kong
    Replies:
    5
    Views:
    246
    Rick DeNatale
    Aug 12, 2006
  3. Stanley Xu
    Replies:
    2
    Views:
    618
    Stanley Xu
    Mar 23, 2011
  4. PerlFAQ Server
    Replies:
    0
    Views:
    395
    PerlFAQ Server
    Jan 25, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    477
    PerlFAQ Server
    Apr 13, 2011
Loading...

Share This Page