你会如何将这个JavaScript正则表达式翻译成Java? [英] How would you translate this JavaScript regex to Java?

查看:61
本文介绍了你会如何将这个JavaScript正则表达式翻译成Java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何将此JavaScript正则表达式翻译为Java?

How would you translate this JavaScript regex to Java?

它从字符串中删除标点符号:

It removes punctuation from a string:

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


推荐答案

如果您希望这与所有标点符号一起使用而不是只需ASCII,你需要使用:

If you expect this to work with all punctuation instead of just ASCII, you need to use:

String new_string = old_string.replaceAll("[\\pS\\pP]", "");

这是因为你称之为标点符号的一些东西实际上是符号,如我的 uniprops 脚本

That’s because some of the things you are calling punctuation are actually symbols, as revealed by 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

这篇关于你会如何将这个JavaScript正则表达式翻译成Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆