Wie würden Sie diese JavaScript-Regex in Java übersetzen?Wie würden Sie diese JavaScript-Regex in Java übersetzen?
Es entfernt Interpunktion aus einem String:
strippedStr = str.replace(/[\.,-\/#!$%\^&\*;:{}=\-_`~()]/g,"");
Wie würden Sie diese JavaScript-Regex in Java übersetzen?Wie würden Sie diese JavaScript-Regex in Java übersetzen?
Es entfernt Interpunktion aus einem String:
strippedStr = str.replace(/[\.,-\/#!$%\^&\*;:{}=\-_`~()]/g,"");
keinen Java Kerl, aber:
public static final String expression = "[\\s\\p{Punct}]";
Die g
am Ende des regulären Ausdrucks bedeutet, es ist global; Die äquivalente Java String
Methode ist replaceAll()
(die eine Suche regexp und eine Ersetzungszeichenfolge benötigt). Das einzige, was müssen Sie die regexp zu tun ist, sich die \
s entkommen, da die Java-Parser Dinge interpretieren wie \.
, wie Sie ein .
zu entkommen versuchen: dort
String strippedStr = str.replaceAll("[\\.,-\\/#!$%\\^&\\*;:{}=\\-_`~()]", "");
Eine Alternative zu backslashing das und das metacharacter, könnten Sie die '\ Q' metaquoting Übersetzung Flucht verwenden, die Java während der Musterzusammenstellung verarbeitet. Das würde Sie einfach '' [\\ Q., -/#! $%^& * ;: {} = -_ '~() \\ E] "' als erstes Argument für Ihre Funktion verwenden. Außerdem müssten Sie nur dem '-' oder einem anfänglichen'^'in einer eckigen Klammerklasse entkommen. – tchrist
Sie nicht brauchen die s///
Sachen Übergeben Sie einfach den regulären Ausdruck, und hier ist es eine Zeichenklasse.
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:
// !"#$%&'()*+,-./:;<=>[email protected][\]^_`{|}~
String p = s.replaceAll("\\p{Punct}", "");
System.out.println(p);
}
Syntax für Java Regular Expressions hier: http://download.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html#sum
Die Klasse POSIX-Zeichen verwendet oben deckt ein wenig mehr, als Sie haben, so bin ich nicht sicher, ob es Ihren Bedürfnissen entspricht.
.
in der Zeichenklasse entkommen müssen.-
zu Beginn der Zeichenklasse zu bewegen, um es+1, wie es die Fluchten und all :) – Kaitsu
@Joey hat - das ist, was die Java Dokumentation nennt, so habe ich ihre Terminologie. – birryree
@Joey: Nein, das ist falsch. Java-the-sabling-idiot ordnet die POSIX-Klassen wie '[[: punct:]]' Dingen zu, die ** wie Unicode-Eigenschaften aussehen **, aber nicht ** sind. So ist '\ p {Punct}' nur ASCII, nicht einmal das aktuelle Gebietsschema. Sie müssen '\ fP' verwenden, um auf die Kategorie 'Allgemein' von Unicode' \ p {Interpunktion} 'zuzugreifen. Ja, ich denke, es ist wirklich dumm. – tchrist
Würde der Auftrag nicht diese Charakterklasse in prägnanter Weise getan keine besondere Bedeutung hat?
str.replaceAll("[\\W_]", "");
'Fraid nicht. [Javas Zeichenklassen-Shortcuts sind gebrochen, wenn sie auf dem eigenen nativen Zeichensatz von Java verwendet werden!] (Http://stackoverflow.com/questions/4304928/unicode-equivalents-for-w-and-b-in-java-regular-expressions)/4307261 # 4307261) – tchrist
Wenn Sie dies erwarten, statt nur ASCII mit allen Interpunktion zu arbeiten, müssen Sie verwenden:
String new_string = old_string.replaceAll("[\\pS\\pP]", "");
Das ist, weil einige der Dinge, die Sie Interpunktion tatsächlich Symbole nennend, wie offenbart durch 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
Das funktioniert nicht aus Gründen, die ich anderswo kommentiert. Der richtige Weg, ist '\ pP' zu verwenden, aber für' \ s', haben Sie es durch meine passieren [Unicode-Enabling Java-regex Schreibungsfunktion] (http://stackoverflow.com/questions/4304928/unicode -Äquivalente-für-w-und-b-in-java-regular-Ausdrücke/4307261 # 4307261), die jede '\ s' korrigieren' [\ u000A \ u000B \ u000C \ u000d \ u0020 \ u0085 \ u00A0 lesen \ u1680 \ u180E \ U2000 \ u2001 \ u2002 \ u2003 \ u2004 \ u2005 \ u2006 \ u2007 \ U2008 \ u2009 \ u200A \ u2028 \ u2029 \ u202F \ u205F \ u3000] '. – tchrist