English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java에서 정규 표현식을 사용하여 문자열에서 보통자를 제거하는 방법

간단한 문자 클래스 " [] 모든 지정된 문자를 일치시키는 것을 의미합니다. 유니코드 문자^위 문자 클래스에서 부정 기호로 사용되며, 다음 표현식은 b를 제외한 모든 문자(공백과 특수 문자 포함)를 일치시킵니다.

"[^b]"

또한, 다음 표현식은 주어진 입력 문자열의 모든 음소를 일치시킵니다.

"([^aeiouyAEIOUY0-9\\W]+);

그런 다음, replaceAll() 메서드를 사용하여 일치하는 문자를 빈 문자열 ""으로 대체하여 일치하는 문자를 제거할 수 있습니다.

예제1

public class RemovingConstants {
   public static void main( String args[] ) {
      String input = "Hi welc#ome to t$utori$alspoint";
      String regex = "([^aeiouAEIOU0-9\\W]+);
      String result = input.replaceAll(regex, "");
      System.out.println("Result: ");+result);
   }
}

출력 결과

Result: i e#oe o $uoi$aoi

예제2

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RemovingConsonants {
   public static void main( String args[] ) {
      Scanner sc = new Scanner(System.in);
      System.out.println("입력 문자열: ");
      String input = sc.nextLine();
      String regex = "([^aeiouyAEIOUY0-9\\W])";
      String constants = "";
      /모드 객체를 생성합니다
      Pattern pattern = Pattern.compile(regex);
      //문자열 내의 컴파일 모드를 일치시킵니다
      Matcher matcher = pattern.matcher(input);
      //비어 있는 문자열 버퍼를 생성합니다
      StringBuffer sb = new StringBuffer();
      while (matcher.find()) {
         matcher.appendReplacement(sb, "");
      }
      matcher.appendTail(sb);
      System.out.println("Result: \
");+ sb.toString());
   }
}

출력 결과

입력 문자열:
# Hello how are you welcome to ooo #
Result:
# eo o ae you eoe o ooo #
당신이 좋아할 수 있는 것