• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regular expression  RSS feed

 
Shaik Riyaz
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know why these piece of code snippets eliminating the "f" and "," from the variable? The first outputs "temp"and "st" which is what I would expect. The second outputs "string2" which implies the regular expression doesn't match:

-------------------------------------------------------
public static String formatString() {
String s="��������abcdefghijklmnopqrstuv,,,,,,,,,!``~!@#$%^&*()--+={}[]|\'':;?/><.,wxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()----+=:;?.,1234567890";
String temp = Normalizer.normalize(s, Normalizer.DECOMP, 0);
String st=temp.replaceAll("[^\\p{ASCII}]","");
String string2=st.replaceAll("[\\x01-\\x08,\\x0b-\\x0c,\\x0e-\\x1f,\\x7f-\\xffff]", "");
return string2;


}


-------------------------------------------------------
 
Bill Cruise
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The function removes the "f" and "," characters because you're telling it to. The first argument to the String replaceAll method is a regular expression. You have commas separating ranges of characters, so the comma is removed. You also have "\\xffff" at the end of the regular expression, so the f is removed. That should be "\\xff", the last two characters are just interpreted as "f". You can fix this by removing the commas and the last two "f"s from the second call to replaceAll.


Some people, when confronted with a problem, think �I know, I�ll use regular expressions.� Now they have two problems.
�Jamie Zawinski, in comp.lang.emacs
 
Shaik Riyaz
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bill Cruise:
The function removes the "f" and "," characters because you're telling it to. The first argument to the String replaceAll method is a regular expression. You have commas separating ranges of characters, so the comma is removed. You also have "\\xffff" at the end of the regular expression, so the f is removed. That should be "\\xff", the last two characters are just interpreted as "f". You can fix this by removing the commas and the last two "f"s from the second call to replaceAll.




Thanks for replying me.It is working fine for "f " character.But comma we are using to differentiate different expression.Do you think that it would be a valid expression to remove it ??Please suggest.

Is there anyway that we can include comma within expression without removing it from expression?
 
Bill Cruise
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need the commas to separate the character ranges in a regular expression.



works just fine.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!