2016-04-01 5 views
0

.txtファイルを使用してString []を取得しようとしていますが、いくつかの例外を除いてすべての句読点を削除する必要があります。ここに私のコードは次のとおりです。replaceAll()これらの例外ですべての句読点を削除します。

replaceAll("[^a-zA-Z ]", ""); 

例外: 1.hyphen(s)は言葉の中にあります。 2.数字を含む単語を取り除く。3.単語の末尾に2つの句読点を含む。

+0

を私は、私を使用しよう私は部分的に働いていますが、単語の中にあるハイフンも取り除きます。 – zzz

答えて

0

[^ a-zA-Z]は文字クラスである。これは、1つの文字と一致することを意味し、この場合、a-z、A-Zまたは空白以外のものと一致します。

単語を照合する場合は、+などの数量指定子を含む文字クラスを使用する必要があります。異なるパターンを一致させる場合は、論理演算子|を適用する必要があります。

これを知ると、1つ以上の数字で終わる単語または中間の数字を持つ単語と一致するようになりました。[^a-zA-Z ][0-9]+|[^a-zA-Z ]+[0-9]これは学校の割り当てのように聞こえるので、私はあなたの3つのケースに適用するための練習としてそれをあなたに残します。

+0

は働いていません。結果は同じです – zzz

+0

大丈夫、クール!ありがとよ! – zzz

0

私は非常に複雑な正規表現を持っていますが、動作します。

\S*\d+\S*|\p{Punct}{2,}\S*|\S*\p{Punct}{2,}|[\p{Punct}&&[^-]]+|(?<![a-z])\-(?![a-z]) 

説明:

Match this alternative «\S*\d+\S*» 
    Match a single character that is NOT a “whitespace character” «\S*» 
     Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*» 
    Match a single character that is a “digit” «\d+» 
     Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» 
    Match a single character that is NOT a “whitespace character” «\S*» 
     Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*» 
Or match this alternative «\p{Punct}{2,}\S*» 
    Match a character from the POSIX character class “punct” «\p{Punct}{2,}» 
     Between 2 and unlimited times, as many times as possible, giving back as needed (greedy) «{2,}» 
    Match a single character that is NOT a “whitespace character” «\S*» 
     Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*» 
Or match this alternative «\S*\p{Punct}{2,}» 
    Match a single character that is NOT a “whitespace character” «\S*» 
     Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*» 
    Match a character from the POSIX character class “punct” «\p{Punct}{2,}» 
     Between 2 and unlimited times, as many times as possible, giving back as needed (greedy) «{2,}» 
Or match this alternative «[\p{Punct}&&[^-]]+» 
    Match a single character present in the list below «[\p{Punct}&&[^-]]+» 
     Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» 
     A character from the POSIX character class “punct” «\p{Punct}» 
     Except the literal character “-” «&&[^-]» 
Or match this alternative «(?<![a-z])\-(?![a-z])» 
    Assert that it is impossible to match the regex below with the match ending at this position (negative lookbehind) «(?<![a-z])» 
     Match a single character in the range between “a” and “z” «[a-z]» 
    Match the character “-” literally «\-» 
    Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?![a-z])» 
     Match a single character in the range between “a” and “z” «[a-z]» 

例:上記

String text ="a-b ab--- - ---a --- , ++++ ?%# $22 43 4zzv"; 

String rx = "(?i)\\S*\\d+\\S*|\\p{Punct}{2,}\\S*|\\S*\\p{Punct}{2,}|[\\p{Punct}&&[^-]]+|(?<![a-z])\\-(?![a-z])"; 

String result = text.replaceAll(rx, " ").trim(); 

System.out.println(result); 

コードが印刷されます:

a-b 
+0

" - "、 " - " + word – zzz

+0

とにかく、ありがとうございました! – zzz

+0

各キャラクターの機能は何ですか?私はまだそこに詰まっています – zzz

関連する問題