English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
정규 표현식 문자 " \\ s 와 주어진 문자열의 공백 문자와 일치합니다。
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example { public static void main(String args[]) { //Reading String from user System.out.println("Enter a String"); Scanner sc = new Scanner(System.in); String input = sc.nextLine(); String regex = "\\s"; //정규 표현식 컴파일 Pattern pattern = Pattern.compile(regex); //matcher 객체 가져오기 Matcher matcher = pattern.matcher(input); int count = 0; while(matcher.find()) { count++; } System.out.println("공백 수: ");+count); } }
출력 결과
문자열을 입력하세요 Hello how are you welcome to w3codebox 공백 수: 6
import java.util.Scanner; public class RegexExample { public static void main( String args[] ) { //정규 표현식 String regex = "\\s"+"; System.out.println("입력 값을 입력하세요: "); Scanner sc = new Scanner(System.in); String input = sc.nextLine(); String result = input.replaceAll(regex, ""); System.out.println("결과: " );+result); } }
출력 결과
입력 값을 입력하세요: hello how are you 결과: hellohowareyou