如何将这个JavaScript正则表达式翻译成Java?
如何将这个JavaScript正则表达式翻译成Java 它从字符串中删除标点符号:如何将这个JavaScript正则表达式翻译成Java?,java,regex,Java,Regex,如何将这个JavaScript正则表达式翻译成Java 它从字符串中删除标点符号: strippedStr = str.replace(/[\.,-\/#!$%\^&\*;:{}=\-_`~()]/g,""); 不是爪哇人,而是: public static final String expression = "[\\s\\p{Punct}]"; 来源:regexp末尾的g表示它是全局的;等效的JavaString方法是(它接受一个搜索regexp和一个替换字符串)。您需要对rege
strippedStr = str.replace(/[\.,-\/#!$%\^&\*;:{}=\-_`~()]/g,"");
不是爪哇人,而是:
public static final String expression = "[\\s\\p{Punct}]";
来源:regexp末尾的
g
表示它是全局的;等效的JavaString
方法是(它接受一个搜索regexp和一个替换字符串)。您需要对regexp本身做的唯一一件事就是转义\
s,因为当您试图转义
时,Java解析器将解释类似\.
的内容:
String strippedStr = str.replaceAll("[\\.,-\\/#!$%\\^&\\*;:{}=\\-_`~()]", "");
这里不需要
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);
}
publicstaticvoidmain(字符串[]args)
{
字符串s=“,/#!$%^&*;:{}=-`~()。/#你好#&%-#($”;
字符串n=s.replaceAll(“[-,/#!$%^&*;:{}=”,“);
System.out.println(n);//应该打印“hello”
//使用与以下任一项匹配的POSIX字符类:
// !"#$%&'()*+,-./:;?@[\]^_`{|}~
字符串p=s.replaceAll(\\p{Punct},“”);
系统输出println(p);
}
此处为Java正则表达式的语法:
上面使用的POSIX字符类比您拥有的稍多,因此我不确定它是否适合您的需要
- 已编辑,因为不需要在字符类中转义
。 - 编辑以将
移动到字符类的开头,因此它没有特殊意义-
str.replaceAll("[\\W_]", "");
如果您希望使用所有标点符号而不是ASCII,则需要使用:
String new_string = old_string.replaceAll("[\\pS\\pP]", "");
这是因为你称之为标点符号的一些东西实际上是符号,如下所示:
@Joey-这就是Java文档对它们的称呼,所以我使用了它们的术语。@Joey:不,这是错误的。Java这个流口水的白痴将POSIX类映射为类似Unicode属性的东西,而不是。所以
\p{punct}
仅为ASCII,甚至不是当前区域设置。您需要使用\fP
访问Unicode\p{标点符号}
General Category。是的,我认为它真的很愚蠢。@Birryee:那不行。看看原因。@Birryee:你把它称为POSIX字符类是相当准确的,因为那就像是一个ASCII类型的函数。不幸的是,它没有人们想要的那么好。这不起作用,因为我同意正确的方法是使用\pP
,但是对于\s
,您必须将其通过my,这将更正任何\s
以读取[\u000A\u000B\u000C\u000D\u0020\u0085\u00A0\u1680\u180E\u20001\u2002\u2003\u2004\u2006\u2007a\u20028\u20029\u2005f]
'Fraid不是。除了反斜杠这个和那个元字符,您可以使用Java在模式编译期间处理的\Q
元引用转换转义。这样您就可以简单地使用“[\\Q,-/\\\!$%^&*:{}=-\\\\\\\\\\\E]“
作为函数的第一个参数。此外,除了方括号字符类中的-
或初始^
之外,不需要转义任何内容。
$ 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