Example
|
Matches
|
[ab]
|
a yoki b lardan bittasi ni oladi a|b ga ekvivalent
|
[ab] [qw]
|
birinchi harf a yoki b ikkinchi harf esa q yoki w
|
[a-d]
|
a dan d gachaga ixtiyoriy bitta belgi
|
^[^a-zA-Z]
|
ixtiyoriy harf bilan boshlansin
|
[a-zA-Z0-9]$
|
ixtiyoriy bitta harf yoki ixtiyoriy raqam bilan tugasin
|
[0-9]%
|
ixtoyoriy bitta raqam va undan keyin % belgisi
|
Short Regex Belgilar
Regex
|
Description
|
.
|
Har qanday belgi
|
\d
|
har qanday raqam [0-9]
|
\D
|
Har qanday raqam bo'lmagan, qisqartmasi [^0-9]
|
\s
|
Har qanday boʻshliq belgisi [\t\n\x0B\f\r]
|
\S
|
Boʻsh joy boʻlmagan har qanday belgi, [^\s] ning qisqartmasi
|
\w
|
Har qanday so'z belgisi [a-zA-Z_0-9]
|
\W
|
Har qanday so'z bo’lmagan belgisi [^\w] ning qisqartmasi
|
\b
|
So'z chegarasi
|
\B
|
So'z chegara emas [^\b] ning qisqartmasi
|
public static void main(String args[]){
System.out.println(Pattern.matches("\\d", "abc")); false
System.out.println(Pattern.matches("\\d", "1")); true
System.out.println(Pattern.matches("\\d", "4443"));
System.out.println(Pattern.matches("\\D+", "abc")); true
System.out.println(Pattern.matches("\\D", "1"));
System.out.println(Pattern.matches("\\D+", "a323abc")); System.out.println(Pattern.matches("\\D", "m"))
System.out.println(Pattern.matches("\\D*", "mak")); true
}
Darsni takrorlab olish uchun
Example
|
Matches
|
^[The]
|
Any string that starts with The ^[The], The
|
of despair$
|
Any string that ends with of despair
|
^abc$
|
A string that starts and ends with abc—an exact match
|
|
|
ab*
|
A string that contains a, followed by zero or more bs—ac, abc, or abbc
|
ab+
|
A string that contains a, followed by one or more bs—abc or abbc, but not ac
|
ab?
|
A string that contains a, followed by zero or one bs—ac or abc, but not abc
|
a?b+$
|
A string that ends with one or more bs, with or without a preceding a; for example, ab, abb, b, or bb, but not aab or aabb
|
|
|
ab{2}
|
A string that contains a, followed by exactly 2 bs—abb
|
ab{2,}
|
A string that contains a, followed by at least 2 bs—abb, abbbb, etc.
|
ab{3,5}
|
A string that contains a, followed by three to five bs—abbb, abbbb, or abbbbb
|
|
|
hi|hello
|
A string that contains either hi or hello
|
(b|cd)ef
|
A string that contains either bef or cdef
|
(a|b)*c
|
A string that has a sequence of alternating as and bs, ending with c
|
|
|
a.[0-9]
|
A string that contains a, followed by any character and a digit
|
^.{3}$
|
Any string of exactly three characters
|
|
|
[ab]
|
A string that contains either a or b; equivalent to a|b
|
[a-d]
|
A string that contains a lowercase a, b, c, or d; equivalent to a|b|c|d or [abcd]
|
^[a-zA-Z]
|
A string that starts with any letter, regardless of case
|
[0-9]%
|
A string that contains any single digit followed by a percent sign
|
[a-zA-Z0-9]$
|
A string that ends with a comma followed by any character
|
|
|
Foydalanilgan adabiyotlar:
https://www.w3schools.com/java/java_regex.asp
https://coderlessons.com/?s=java+regex
https://coderlessons.com/tutorials/java-tekhnologii/izuchite-java-regex/java-regex-interfeis-matchresult
Do'stlaringiz bilan baham: |