English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
이 문자열에서 숫자를 매칭할 수 있는 다음과 같은 정규 표현식을 사용할 수 있습니다-
“\\d+" Or, "([0-9]+)
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ExtractingDigits { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("샘플 텍스트 입력하십시오: "); String data = sc.nextLine(); //정규 표현식으로 문자열 내의 숫자를 매칭 String regex = "\\d+)"; //패턴 객체 생성 Pattern pattern = Pattern.compile(regex); //Matcher 객체 생성 Matcher matcher = pattern.matcher(data); System.out.println("주어진 문자열에 있는 자릿수는: "); while(matcher.find()) { System.out.print(matcher.group())+" "); } } }
출력 결과
샘플 텍스트 입력하십시오: 이것은 샘플입니다 23 텍스트 46 와 11223 그 안의 숫자 주어진 문자열에 있는 자릿수는: 23 46 11223
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Just { public static void main(String[] args) { String data = "abc12def334hjdsk7438dbds3y388)"; //정규 표현식으로 숫자 String regex = "([0-9]+)"; //패턴 객체 생성 Pattern pattern = Pattern.compile(regex); //Matcher 객체 생성 Matcher matcher = pattern.matcher(data); System.out.println("주어진 문자열에 있는 자릿수는: "); while(matcher.find()) { System.out.print(matcher.group())+" "); } } }
출력 결과
주어진 문자열에 있는 자릿수는: 12 334 7438 3 388