您如何将此JavaScript正则表达式翻译为Java?
它从字符串中删除标点:
strippedStr = str.replace(/[\.,-\/#!$%\^&\*;:{}=\-_`~()]/g,"");
答案 0 :(得分:1)
不需要那里的s///
内容,你只需要传递正则表达式,这里就是一个字符类。
public static void main(String [] args)
{
String s = ".,/#!$%^&*;:{}=-_`~()./#hello#&%---#(($";
String n = s.replaceAll("[-.,/#!$%^&*;:{}=_`~()]", "");
System.out.println(n); // should print "hello"
// using POSIX character class which matches any of:
// !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
String p = s.replaceAll("\\p{Punct}", "");
System.out.println(p);
}
Java正则表达式的语法:http://download.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html#sum
上面使用的POSIX字符类比你拥有的多一点,所以我不确定它是否符合你的需要。
.
。 -
移至字符类的开头,因此没有特殊含义 答案 1 :(得分:1)
如果您希望这适用于所有标点符号而不仅仅是ASCII,则需要使用:
String new_string = old_string.replaceAll("[\\pS\\pP]", "");
那是因为你称之为标点符号的一些事实上是符号,正如my uniprops script所揭示的那样:
$ uniprops - \\ . , / '#' ! '$' % ^ '&' '*' ';' : { } = _ '`' '~' '(' ')'
U+002D ‹-› \N{ HYPHEN-MINUS }:
\pP \p{Pd}
All Any ASCII Assigned Common Zyyy Dash Dash_Punctuation Pd P Gr_Base Grapheme_Base Graph GrBase Punct Pat_Syn Pattern_Syntax PatSyn PosixGraph PosixPrint PosixPunct Print Punctuation XPosixGraph XPosixPrint XPosixPunct
U+005C ‹\› \N{ REVERSE SOLIDUS }:
\pP \p{Po}
All Any ASCII Assigned Common Zyyy Po P Gr_Base Grapheme_Base Graph GrBase Other_Punctuation Punct Pat_Syn Pattern_Syntax PatSyn PosixGraph PosixPrint PosixPunct Print Punctuation XPosixGraph XPosixPrint XPosixPunct
U+002E ‹.› \N{ FULL STOP }:
\pP \p{Po}
All Any ASCII Assigned Case_Ignorable CI Common Zyyy Po P Gr_Base Grapheme_Base Graph GrBase Other_Punctuation Punct Pat_Syn Pattern_Syntax PatSyn PosixGraph PosixPrint PosixPunct Print Punctuation STerm Term Terminal_Punctuation XPosixGraph XPosixPrint XPosixPunct
U+002C ‹,› \N{ COMMA }:
\pP \p{Po}
All Any ASCII Assigned Common Zyyy Po P Gr_Base Grapheme_Base Graph GrBase Other_Punctuation Punct Pat_Syn Pattern_Syntax PatSyn PosixGraph PosixPrint PosixPunct Print Punctuation Term Terminal_Punctuation XPosixGraph XPosixPrint XPosixPunct
U+002F ‹/› \N{ SOLIDUS }:
\pP \p{Po}
All Any ASCII Assigned Common Zyyy Po P Gr_Base Grapheme_Base Graph GrBase Other_Punctuation Punct Pat_Syn Pattern_Syntax PatSyn PosixGraph PosixPrint PosixPunct Print Punctuation XPosixGraph XPosixPrint XPosixPunct
U+0023 ‹#› \N{ NUMBER SIGN }:
\pP \p{Po}
All Any ASCII Assigned Common Zyyy Po P Gr_Base Grapheme_Base Graph GrBase Other_Punctuation Punct Pat_Syn Pattern_Syntax PatSyn PosixGraph PosixPrint PosixPunct Print Punctuation XPosixGraph XPosixPrint XPosixPunct
U+0021 ‹!› \N{ EXCLAMATION MARK }:
\pP \p{Po}
All Any ASCII Assigned Common Zyyy Po P Gr_Base Grapheme_Base Graph GrBase Other_Punctuation Punct Pat_Syn Pattern_Syntax PatSyn PosixGraph PosixPrint PosixPunct Print Punctuation STerm Term Terminal_Punctuation XPosixGraph XPosixPrint XPosixPunct
U+0024 ‹$› \N{ DOLLAR SIGN }:
\pS \p{Sc}
All Any ASCII Assigned Common Zyyy Currency_Symbol Sc S Gr_Base Grapheme_Base Graph GrBase Pat_Syn Pattern_Syntax PatSyn PosixGraph PosixPrint PosixPunct Print Symbol XPosixGraph XPosixPrint XPosixPunct
U+0025 ‹%› \N{ PERCENT SIGN }:
\pP \p{Po}
All Any ASCII Assigned Common Zyyy Po P Gr_Base Grapheme_Base Graph GrBase Other_Punctuation Punct Pat_Syn Pattern_Syntax PatSyn PosixGraph PosixPrint PosixPunct Print Punctuation XPosixGraph XPosixPrint XPosixPunct
U+005E ‹^› \N{ CIRCUMFLEX ACCENT }:
\pS \p{Sk}
All Any ASCII Assigned Case_Ignorable CI Common Zyyy Dia Diacritic Sk S Gr_Base Grapheme_Base Graph GrBase Math Modifier_Symbol Pat_Syn Pattern_Syntax PatSyn PosixGraph PosixPrint PosixPunct Print Symbol XPosixGraph XPosixPrint XPosixPunct
U+0026 ‹&› \N{ AMPERSAND }:
\pP \p{Po}
All Any ASCII Assigned Common Zyyy Po P Gr_Base Grapheme_Base Graph GrBase Other_Punctuation Punct Pat_Syn Pattern_Syntax PatSyn PosixGraph PosixPrint PosixPunct Print Punctuation XPosixGraph XPosixPrint XPosixPunct
U+002A ‹*› \N{ ASTERISK }:
\pP \p{Po}
All Any ASCII Assigned Common Zyyy Po P Gr_Base Grapheme_Base Graph GrBase Other_Punctuation Punct Pat_Syn Pattern_Syntax PatSyn PosixGraph PosixPrint PosixPunct Print Punctuation XPosixGraph XPosixPrint XPosixPunct
U+003B ‹;› \N{ SEMICOLON }:
\pP \p{Po}
All Any ASCII Assigned Common Zyyy Po P Gr_Base Grapheme_Base Graph GrBase Other_Punctuation Punct Pat_Syn Pattern_Syntax PatSyn PosixGraph PosixPrint PosixPunct Print Punctuation Term Terminal_Punctuation XPosixGraph XPosixPrint XPosixPunct
U+003A ‹:› \N{ COLON }:
\pP \p{Po}
All Any ASCII Assigned Case_Ignorable CI Common Zyyy Po P Gr_Base Grapheme_Base Graph GrBase Other_Punctuation Punct Pat_Syn Pattern_Syntax PatSyn PosixGraph PosixPrint PosixPunct Print Punctuation Term Terminal_Punctuation XPosixGraph XPosixPrint XPosixPunct
U+007B ‹{› \N{ LEFT CURLY BRACKET }:
\pP \p{Ps}
All Any ASCII Assigned Bidi_M Bidi_Mirrored BidiM Common Zyyy Ps P Gr_Base Grapheme_Base Graph GrBase Open_Punctuation Punct Pat_Syn Pattern_Syntax PatSyn PosixGraph PosixPrint PosixPunct Print Punctuation XPosixGraph XPosixPrint XPosixPunct
U+007D ‹}› \N{ RIGHT CURLY BRACKET }:
\pP \p{Pe}
All Any ASCII Assigned Bidi_M Bidi_Mirrored BidiM Close_Punctuation Pe Common Zyyy P Gr_Base Grapheme_Base Graph GrBase Punct Pat_Syn Pattern_Syntax PatSyn PosixGraph PosixPrint PosixPunct Print Punctuation XPosixGraph XPosixPrint XPosixPunct
U+003D ‹=› \N{ EQUALS SIGN }:
\pS \p{Sm}
All Any ASCII Assigned Common Zyyy Sm S Gr_Base Grapheme_Base Graph GrBase Math Math_Symbol Pat_Syn Pattern_Syntax PatSyn PosixGraph PosixPrint PosixPunct Print Symbol XPosixGraph XPosixPrint XPosixPunct
U+005F ‹_› \N{ LOW LINE }:
\w \pP \p{Pc}
All Any ASCII Assigned Common Zyyy Connector_Punctuation Pc P Gr_Base Grapheme_Base Graph GrBase ID_Continue IDC Punct PerlWord PosixGraph PosixPrint PosixPunct PosixWord Print Punctuation Word XID_Continue XIDC XPosixGraph XPosixPrint XPosixPunct XPosixWord
U+0060 ‹`› \N{ GRAVE ACCENT }:
\pS \p{Sk}
All Any ASCII Assigned Case_Ignorable CI Common Zyyy Dia Diacritic Sk S Gr_Base Grapheme_Base Graph GrBase Modifier_Symbol Pat_Syn Pattern_Syntax PatSyn PosixGraph PosixPrint PosixPunct Print Symbol XPosixGraph XPosixPrint XPosixPunct
U+007E ‹~› \N{ TILDE }:
\pS \p{Sm}
All Any ASCII Assigned Common Zyyy Sm S Gr_Base Grapheme_Base Graph GrBase Math Math_Symbol Pat_Syn Pattern_Syntax PatSyn PosixGraph PosixPrint PosixPunct Print Symbol XPosixGraph XPosixPrint XPosixPunct
U+0028 ‹(› \N{ LEFT PARENTHESIS }:
\pP \p{Ps}
All Any ASCII Assigned Bidi_M Bidi_Mirrored BidiM Common Zyyy Ps P Gr_Base Grapheme_Base Graph GrBase Open_Punctuation Punct Pat_Syn Pattern_Syntax PatSyn PosixGraph PosixPrint PosixPunct Print Punctuation XPosixGraph XPosixPrint XPosixPunct
U+0029 ‹)› \N{ RIGHT PARENTHESIS }:
\pP \p{Pe}
All Any ASCII Assigned Bidi_M Bidi_Mirrored BidiM Close_Punctuation Pe Common Zyyy P Gr_Base Grapheme_Base Graph GrBase Punct Pat_Syn Pattern_Syntax PatSyn PosixGraph PosixPrint PosixPunct Print Punctuation XPosixGraph XPosixPrint XPosixPunct
答案 2 :(得分:0)
不是Java家伙,但是:
public static final String expression = "[\\s\\p{Punct}]";
答案 3 :(得分:0)
正则表达式末尾的g
表示它是全局的;等效的Java String
方法是replaceAll()
(它采用搜索regexp和替换字符串)。你需要对regexp本身做的唯一事情就是转义\
,因为当你试图逃避\.
时,Java解析器会解释.
之类的东西:
String strippedStr = str.replaceAll("[\\.,-\\/#!$%\\^&\\*;:{}=\\-_`~()]", "");
答案 4 :(得分:0)
这个角色类不会以更简洁的方式完成工作吗?
str.replaceAll("[\\W_]", "");