Connecting Tech Pros Worldwide Forums | Help | Site Map

java.util.regex.PatternSyntaxException

Needs Regular Fix
 
Join Date: Nov 2007
Location: Cebu City, Philippines
Posts: 511
#1: Jun 21 '08
This was my first time to encouter this kind of exception....

that exception appears when i invoked the the method below.

Expand|Select|Wrap|Line Numbers
  1. private final String encrypting(String enc){
  2.         int declen=array1.length;
  3.         String temp=enc;
  4.         for(int x=-1;x++<declen;){
  5.             temp=temp.replaceAll(String.valueOf(array2[x]),String
  6.                     .valueOf(array1[x]));
  7.         }
  8.         return temp;
  9.     }
array1 contains a character ' * ' or a multiply operator in java.
@ runtime, it throws java.util.regex.PatternSyntaxException

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.sequence(Unknown Source)
at java.util.regex.Pattern.expr(Unknown Source)
at java.util.regex.Pattern.compile(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's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Jun 21 '08

re: java.util.regex.PatternSyntaxException


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
Needs Regular Fix
 
Join Date: Nov 2007
Location: Cebu City, Philippines
Posts: 511
#3: Jun 21 '08

re: java.util.regex.PatternSyntaxException


Quote:

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?
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4: Jun 21 '08

re: java.util.regex.PatternSyntaxException


Quote:

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
Needs Regular Fix
 
Join Date: Nov 2007
Location: Cebu City, Philippines
Posts: 511
#5: Jun 21 '08

re: java.util.regex.PatternSyntaxException


Quote:
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...
Reply