2016-04-01 33 views
0

Bir .txt dosyası kullanarak bir String [] almaya çalışıyorum ve bazı özel durumlarla tüm noktalama işaretlerini kaldırmam gerekiyor.replaceAll() Tüm noktalama işaretlerini bu istisnalarla silme

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

istisnalar: İşte benim kodudur 1.hyphen (lar) içinde bir kelimeyi olduğunu. 2. Rakamlar içeren sözcüklerden kurtulun 3. Kelimelerin silinmesi sonunda iki noktalama işareti ve başlangıçta

+0

i i kullanmayı deneyin t, ve kısmen çalışır, ama bir kelimenin içindeki tire (ler) den de kurtulacağım. – zzz

cevap

0

[^ a-zA-Z] bir karakter sınıfıdır. Bu, yalnızca bir karakterle eşleşeceği anlamına gelir ve bu durumda, a-z, A-Z veya bir boşluk olmayan herhangi bir şeyle eşleşir.

Sözcükleri eşleştirmek istiyorsanız, örneğin + ile niceleyicilerle karakter sınıflarını kullanmanız gerekir. Farklı modelleri eşleştirmek istiyorsanız, | mantıksal operatörünü uygulamanız gerekir.

Bunu biliyorsanız, bir veya daha fazla sayıyla biten veya ortada [^a-zA-Z ][0-9]+|[^a-zA-Z ]+[0-9] numaralı bir numaraya sahip kelimeleri eşleştirebilirsiniz. Bu, bir okul görevine benzediğinden üç kasanın için uygulamak için bir egzersiz olarak size bırakacağım.

+0

arkadaş çalışmıyor. Sonuç aynı – zzz

+0

gösterir, harika! Teşekkür ederim ortak! – zzz

0

Çok karmaşık bir düzenli ilişkim var ancak çalışıyor.

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

Açıklama:

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]» 

Örnek:

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); 

Kod yukarıdaki yazdırır:

a-b 
+0

"-" kalıyor ve "-" + kelime – zzz

+0

yine de teşekkürler! – zzz

+0

Her karakterin işlevi nedir? hala orada – zzz

İlgili konular