How would you translate this JavaScript regex to Java? -


how translate javascript regex java?

it removes punctuation string:

strippedstr = str.replace(/[\.,-\/#!$%\^&\*;:{}=\-_`~()]/g,""); 

don't need s/// stuff in there, pass regular expression, , here it's character class.

public static void main(string [] args) {     string s = ".,/#!$%^&*;:{}=-_`~()./#hello#&%---#(($";     string n = s.replaceall("[-.,/#!$%^&*;:{}=_`~()]", "");     system.out.println(n); // should print "hello"      // using posix character class matches of:     // !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~     string p = s.replaceall("\\p{punct}", "");     system.out.println(p); } 

syntax java regular expressions here: http://download.oracle.com/javase/6/docs/api/java/util/regex/pattern.html#sum

the posix character class used above covers little more have, i'm not sure if fits needs.

  • edited because don't need escape . in character class.
  • edited move - beginning of character class has no special meaning

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -