Java 정규식을 사용하여 모든 대문자를 문자열 끝으로 이동

王林
풀어 주다: 2023-08-25 13:21:20
앞으로
924명이 탐색했습니다.

Java 정규식을 사용하여 모든 대문자를 문자열 끝으로 이동

하위 표현식 "[ ]"은 괄호 안에 지정된 모든 문자와 일치합니다. 따라서 모든 대문자를 문자열 끝으로 이동하려면 다음 단계를 수행해야 합니다.

  • 주어진 문자열의 모든 문자를 반복합니다.

  • 주어진 문자열의 모든 대문자를 일치시키려면 "[A-Z]" 정규식을 사용하세요.

  • 특수 문자와 나머지 문자를 두 개의 다른 문자열로 연결합니다.

  • 마지막으로 특수 문자열을 다른 문자열로 연결합니다.

예제 1

public class RemovingSpecialCharacters { public static void main(String args[]) { String input = "sample B text C with G upper case LM characters in between"; String regex = "[A-Z]"; String specialChars = ""; String inputData = ""; for(int i=0; i< input.length(); i++) { char ch = input.charAt(i); if(String.valueOf(ch).matches(regex)) { specialChars = specialChars + ch; } else { inputData = inputData + ch; } } System.out.println("Result: "+inputData+specialChars); } }
로그인 후 복사

Output

Result: sample text with upper case characters in betweenBCGLM
로그인 후 복사

예제 2

다음은 Regex 패키지 방식을 사용하여 대문자를 이동하는 Java 프로그램입니다. 끝까지 문자열.

import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test { public static void main(String args[]) { String input = "sample B text C with G upper case LM characters in between"; String regex = "[A-Z]"; String specialChars = ""; System.out.println("Input string: \n"+input); //Creating a pattern object Pattern pattern = Pattern.compile(regex); //Matching the compiled pattern in the String Matcher matcher = pattern.matcher(input); //Creating an empty string buffer StringBuffer sb = new StringBuffer(); while (matcher.find()) { specialChars = specialChars+matcher.group(); matcher.appendReplacement(sb, ""); } matcher.appendTail(sb); System.out.println("Result: \n"+ sb.toString()+specialChars ); } }
로그인 후 복사

Output

Input string: sample B text C with G upper case LM characters in between Result: sample text with upper case characters in betweenBCGLM
로그인 후 복사

위 내용은 Java 정규식을 사용하여 모든 대문자를 문자열 끝으로 이동의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:tutorialspoint.com
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!