Java Regex의 matches()와 find()의 차이
와의 차이를 이해하려고 합니다.
(아는 ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★」matches()
있는 또, 「 」, 「 」, 「 」, 「 」, 「 」, 「 」, 「 」, 「 」, 「 」, 「 」, 「 」, 「 」, 「 」, 「 」, 「 」, 「 」, 「 」, 「 」, 「 」, 「 」find()
이치
그 맞다면, 저는 당신이 알 수.matches()
find()
검색된 일치 수를 카운트하는 경우를 제외하고.
에는 String이 있어야 .find()
matches()
기본 제공 방식입니다.
요약하면:
- 제 추정이 맞습니까?
- 언제가 편리합니까?
matches()
find()
matches
으로 "행렬"을 하려고 합니다.^
$
것을 합니다.따라서 이 코드의 출력은 다음과 같습니다.
public static void main(String[] args) throws ParseException {
Pattern p = Pattern.compile("\\d\\d\\d");
Matcher m = p.matcher("a123b");
System.out.println(m.find());
System.out.println(m.matches());
p = Pattern.compile("^\\d\\d\\d$");
m = p.matcher("123");
System.out.println(m.find());
System.out.println(m.matches());
}
/* output:
true
false
true
true
*/
123
의 서브스트링입니다.a123b
에, so sofind()
방법 matches()
만 '아'만a123b
않다123
거짓
matches
문자열 전체가 지정된 패턴과 일치할 경우 true를 반환합니다. find
는 패턴과 일치하는 서브스트링을 찾으려고 합니다.
matches()
- 전체 문자열이 일치하는 경우에만 true가 반환됩니다.find()
- regex와 일치하는 다음 항목을 부분 문자열 내에서 찾습니다.
다음과 같은 경우에 "다음"을 강조합니다.find()
, 호출한 find()
을 사용하다 「」를 사용해 .find()
해서 '어느끼다'라고 불러도 요.start()
서브스트링이 일치한 위치를 반환합니다.
예
final Matcher subMatcher = Pattern.compile("\\d+").matcher("skrf35kesruytfkwu4ty7sdfs");
System.out.println("Found: " + subMatcher.matches());
System.out.println("Found: " + subMatcher.find() + " - position " + subMatcher.start());
System.out.println("Found: " + subMatcher.find() + " - position " + subMatcher.start());
System.out.println("Found: " + subMatcher.find() + " - position " + subMatcher.start());
System.out.println("Found: " + subMatcher.find());
System.out.println("Found: " + subMatcher.find());
System.out.println("Matched: " + subMatcher.matches());
System.out.println("-----------");
final Matcher fullMatcher = Pattern.compile("^\\w+$").matcher("skrf35kesruytfkwu4ty7sdfs");
System.out.println("Found: " + fullMatcher.find() + " - position " + fullMatcher.start());
System.out.println("Found: " + fullMatcher.find());
System.out.println("Found: " + fullMatcher.find());
System.out.println("Matched: " + fullMatcher.matches());
System.out.println("Matched: " + fullMatcher.matches());
System.out.println("Matched: " + fullMatcher.matches());
System.out.println("Matched: " + fullMatcher.matches());
산출량
Found: false
Found: true - position 4
Found: true - position 17
Found: true - position 20
Found: false
Found: false
Matched: false
-----------
Found: true - position 0
Found: false
Found: false
Matched: true
Matched: true
Matched: true
Matched: true
전화할 때 하세요.find()
「 」, 「 」의 는, 입니다.Matcher
가 regex로 리셋되지 .^
★★★★★★★★★★★★★★★★★」$
전체 문자열과 일치합니다.
find()
서 정규 표현은 「」입니다.matches()
완전한 표현을 고려합니다.
find()
는 식의 서브스트링이 패턴과 일치하는 경우에만 true를 반환합니다.
public static void main(String[] args) {
Pattern p = Pattern.compile("\\d");
String candidate = "Java123";
Matcher m = p.matcher(candidate);
if (m != null){
System.out.println(m.find());//true
System.out.println(m.matches());//false
}
}
matches();
만, 「버퍼링」이라고 것은 아닙니다.find()
버퍼를 지정합니다. find()
먼저 의 끝에 인덱스를 합니다.
그렇기 때문에 당신이 이런 코드를 가지고 있을 때
1:Pattern.compile("[a-z]");
2:Pattern.matcher("0a1b1c3d4");
3:int count = 0;
4:while(matcher.find()){
5:count++: }
At 4: 패턴 구조를 사용하는 regex 엔진은 코드 전체를 읽습니다.regex[single character]
일치하는 항목을 하나 이상 찾습니다.이러한 일치가 발견되면 인덱스화 됩니다.인덱스화된 결과에 따라 루프가 실행됩니다.그렇지 않으면 다음과 같은 계산이 선행되지 않으면 루프가 실행됩니다.matches()
;는 그렇지 않습니다.일치하는 문자열의 첫 번째 문자가 알파벳이 아니기 때문에 while 문은 실행되지 않습니다.
언급URL : https://stackoverflow.com/questions/4450045/difference-between-matches-and-find-in-java-regex
'programing' 카테고리의 다른 글
Vuetify.js - 동적 카드 높이 애니메이션 (0) | 2022.07.21 |
---|---|
열거값을 int로 변환하는 방법 (0) | 2022.07.21 |
Java에서 파라미터를 참조로 전달할 수 있습니까? (0) | 2022.07.16 |
exec() 함수와 그 패밀리를 설명해 주세요. (0) | 2022.07.16 |
Electron-Vue에 기반한 Electron 앱의 Vuex 돌연변이의 이상한 동작 (0) | 2022.07.16 |