java.util.regex.PatternSyntaxException

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #1

    java.util.regex.PatternSyntaxException

    This was my first time to encouter this kind of exception....

    that exception appears when i invoked the the method below.

    [CODE=JAVA]private final String encrypting(Stri ng enc){
    int declen=array1.l ength;
    String temp=enc;
    for(int x=-1;x++<declen;){
    temp=temp.repla ceAll(String.va lueOf(array2[x]),String
    .valueOf(array1[x]));
    }
    return temp;
    }[/CODE]

    array1 contains a character ' * ' or a multiply operator in java.
    @ runtime, it throws java.util.regex .PatternSyntaxE xception

    same as array2, but another array of characters.

    Dangling meta character ' * ' near index 0 *
    @at java.util.regex .Pattern.error( Unknown Source)
    at java.util.regex .Pattern.sequen ce(Unknown Source)
    at java.util.regex .Pattern.expr(U nknown Source)
    at java.util.regex .Pattern.compil e(Unknown Source)

    How can i reproduce this method? I don't have any idea how to set the asterisk to be treaten as ordinary character since it has special meanings according to this article....

    looking forward for your replies,
    sukatoa
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    You are trying to use the single character string "*" as a regular expression.
    It isn't one. The star character is a reflexive closure meaning: zero or more
    times the previous regular expression but there isn't any, hence the exception
    you saw when you executed that code fragment.

    kind regards,

    Jos

    Comment

    • sukatoa
      Contributor
      • Nov 2007
      • 539

      #3
      Originally posted by JosAH
      You are trying to use the single character string "*" as a regular expression.
      It isn't one. The star character is a reflexive closure meaning: zero or more
      times the previous regular expression but there isn't any, hence the exception
      you saw when you executed that code fragment.

      kind regards,

      Jos
      Thanks for your reply Jos,

      Yah, i was trying to do it like that....

      @ the given method,
      Any askterisk that may exists on a string should be replaced by another character let say 'E'....

      How can i set that character to be an ordinary character? Is it possible?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by sukatoa
        How can i set that character to be an ordinary character? Is it possible?
        Escape it by using a back slash; note that the backslash itself is special to the
        javac compiler so you have to escape it again in literal strings: i.e. "\\*" would
        result in a one character regular expression that matches the star character.

        kind regards,

        Jos

        Comment

        • sukatoa
          Contributor
          • Nov 2007
          • 539

          #5
          How can i set that character to be an ordinary character? Is it possible?
          Hhmmmhhh.... In general, that should be impossible to implement....
          Im trying to avoid double backslash to block the else/if statement inside the
          method.....

          and i realize that also the ( and ) is also a metacharacter.. . :(

          I have no choice.... :)

          Thanks for your time Jos...

          Comment

          Working...