수색…


[0-9] 또는 \ d (Java)를 사용하여 한자리 숫자 문자 일치

[0-9]\d 는 정규 표현식 엔진이 유니 코드를 인식하고 \d ②와 같이 일치하지 않는 한 동일한 패턴입니다. 둘 다 한자리 숫자 문자와 일치하므로 읽을 수있는 표기법을 사용할 수 있습니다.

일치시키려는 패턴의 문자열을 만듭니다. \ d 표기법을 사용하는 경우 첫 번째 백 슬래시를 이스케이프하려면 두 번째 백 슬래시를 추가해야합니다.

String pattern = "\\d";

Pattern 객체를 만듭니다. pattern 문자열을 compile () 메서드에 전달합니다.

Pattern p = Pattern.compile(pattern);

Matcher 객체를 만듭니다. 패턴을 찾을 문자열을 matcher () 메서드에 전달합니다. 패턴이 있는지 확인하십시오.

Matcher m1 = p.matcher("0");
m1.matches(); //will return true

Matcher m2 = p.matcher("5");
m2.matches(); //will return true

Matcher m3 = p.matcher("12345");
m3.matches(); //will return false since your pattern is only for a single integer

다양한 숫자 매칭

[ab] 여기서 a와 b는 0 에서 9 숫자입니다.

[3-7] will match a single digit in the range 3 to 7.

여러 자릿수 맞추기

\d\d       will match 2 consecutive digits
\d+        will match 1 or more consecutive digits
\d*        will match 0 or more consecutive digits
\d{3}      will match 3 consecutive digits
\d{3,6}    will match 3 to 6 consecutive digits
\d{3,}     will match 3 or more consecutive digits

위의 예제에서 \d 는 숫자 범위로 바꿀 수 있습니다.

[3-7][3-7]    will match 2 consecutive digits that are in the range 3 to 7
[3-7]+        will match 1 or more consecutive digits that are in the range 3 to 7
[3-7]*        will match 0 or more consecutive digits that are in the range 3 to 7
[3-7]{3}      will match 3 consecutive digits that are in the range 3 to 7
[3-7]{3,6}    will match 3 to 6 consecutive digits that are in the range 3 to 7
[3-7]{3,}     will match 3 or more consecutive digits that are in the range 3 to 7

특정 숫자를 선택할 수도 있습니다.

[13579]       will only match "odd" digits
[02468]       will only match "even" digits
1|3|5|7|9     another way of matching "odd" digits - the | symbol means OR

둘 이상의 숫자가 포함 된 범위의 숫자 일치 :

\d|10        matches 0 to 10    single digit OR 10.  The | symbol means OR
[1-9]|10     matches 1 to 10    digit in range 1 to 9 OR 10
[1-9]|1[0-5] matches 1 to 15    digit in range 1 to 9 OR 1 followed by digit 1 to 5
\d{1,2}|100  matches 0 to 100   one to two digits OR 100

다른 숫자로 나누는 일치하는 숫자 :

\d*0         matches any number that divides by 10  - any number ending in 0
\d*00        matches any number that divides by 100 - any number ending in 00
\d*[05]      matches any number that divides by 5   - any number ending in 0 or 5
\d*[02468]   matches any number that divides by 2   - any number ending in 0,2,4,6 or 8

04, 08, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60으로 끝나는 숫자를 4로 나누는 일치하는 숫자 - 0, , 64, 68, 72, 76, 80, 84, 88, 92 또는 96

[048]|\d*(00|04|08|12|16|20|24|28|32|36|40|44|48|52|56|60|64|68|72|76|80|84|88|92|96)

이것은 단축 될 수 있습니다. 예를 들어, 20|24|28 대신에 2[048] 사용할 수 있습니다. : 40 년대, 60 년대와 80 년대는 동일한 패턴을 가지고 또한, 우리는 그것들을 포함 할 수 있습니다 [02468][048] 과 다른 패턴이 너무 [13579][26] . 따라서 전체 시퀀스를 다음과 같이 줄일 수 있습니다.

[048]|\d*([02468][048]|[13579][26])    - numbers divisible by 4

2,4,5,10 등으로 나눌 수있는 패턴이없는 일치하는 숫자는 항상 간결하게 처리 할 수 ​​없으며 일반적으로 숫자의 범위에 의존해야합니다. 예를 들어 1에서 50까지의 범위에서 7로 나누는 모든 숫자를 일치 시키려면 모든 숫자를 나열하면됩니다.

7|14|21|28|35|42|49

or you could do it this way

7|14|2[18]|35|4[29]

일치하는 앞 / 뒤 공백

후행 공백

\s*$ :이은 (일치 * ) 공백 ( \s ) 말 (에 $ 텍스트)

선행 공백

^\s* :이은 (일치 * ) 공백 ( \s 처음 (에) ^ 텍스트의)

비고

\s 는 여러 RegExp 엔진에 공통된 메타 문자이며 공백 문자 (공백, 줄 바꿈 및 탭)를 캡처하기위한 것입니다. 참고 : 아마도 모든 유니 코드 공간 문자를 캡처 하지는 않습니다 . 이것에 대해 엔진 문서를 확인하십시오.

모든 플로트 일치

[\+\-]?\d+(\.\d*)?

기호가 필요 없거나 방정식을 구문 분석 할 때 부호가있는 부동 소수점과 일치합니다 ( [\+\-]? 그래서 당신은 \d+(\.\d+)?

설명:

  • \d+ 는 모든 정수와 일치합니다.
  • ()? 괄호의 내용이 선택 사항이지만 항상 함께 표시되어야 함을 의미합니다.
  • '\.' '.'와 일치합니다. ','이후 이걸 이스케이프 처리해야합니다. 일반적으로 모든 문자와 일치합니다.

따라서이 표현식은 일치 할 것입니다.

5
+5
-5
5.5
+5.5
-5.5

특정 위치의 단어를 기반으로 목록에서 특정 행 선택

다음 목록이 있습니다.

1. Alon Cohen
2. Elad Yaron
3. Yaron Amrani
4. Yogev Yaron

나는 Yaron 성을 가진 남자들의 이름을 선택하고 싶다.

어떤 숫자인지는 신경 쓰지 않기 때문에 나는 그것을 어떤 숫자이든 그 줄의 처음부터 일치하는 점과 공백으로 채울 것입니다. ^[\d]+\.\s .

이제는 대문자인지 소문자인지를 알 수 없기 때문에 공간과 이름을 일치시켜야합니다. [a-zA-Z]+\s 또는 [aZ]+\s [\w]+\s 수도 있습니다.

이제 우리는 Yaron을 포함하는 줄만 성 (라인 끝에)으로 가져 오는 데 필요한 성을 지정할 것입니다 : \sYaron$ .

이 모든 것을 ^[\d]+\.\s[\w]+\sYaron$ .

실제 예 : https://regex101.com/r/nW4fH8/1



Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow