Süntaks
Tabel annab lühiülevaate regulaaravaldise süntaksist. Täpsemalt võib vaadata näiteks siit
Muster |
info |
Näide |
Tekst (kust otsitakse) |
|
---|---|---|---|---|
Leiab |
Ei leia |
|||
täht ise |
info |
"abc" |
"abc" |
"aba" |
. |
suvaline sümbol |
"a.bc" |
"a#bc" |
"abc" |
^ |
algus |
"^abc" |
"abcde" |
"dabc" |
$ |
lõpp |
"abc$" |
"dabc" |
"abcd" |
? |
0 või üks kord |
"ab?c" |
"ac" "abc" |
"abbc" |
* |
0 või rohkem |
"ab*c" |
"ac" "abc" "abbc" |
"ccbbaa" |
+ |
1 või rohkem |
"ab+c" |
"abc" "abbc" |
"accbcc" "ac" |
{m} |
kordub m korda |
"ab{2}c" |
"aabbcc" "abbc" |
"abc" "abbbc" |
{m, n} |
kordub m kuni n korda |
"ab{2,3}c" |
"abbcc" "abbbcc" |
"aabbbbcc" "abc" |
{m,} |
kordub m kuni ükskõik kui palju korda |
"ab{2,}c” |
“abbc” “abbbbbc” |
"abc" "ac" |
\ |
escape Et kasutada regex erisümboleid otseses tähenduses, näiteks, et "." tähistaks punkti, mitte suvalist tähte |
"ab\.c" |
"ab.c" |
"ab#c" |
[..] |
sobiv hulk hulk, millest otsida |
"[ab]cd" |
"acd" "bcd" |
"ccdc" "cd" |
[..-..] |
sobiv vahemik hulk, millest otsida |
"[a-d]ef" |
"aef" "def" |
"ef" "abc" |
[..\-] |
miinus hulgas |
"[a\-d]ef" |
"def" "-ef" |
"ef-" "cef" |
(..) |
grupeerimine |
"(ab)?cd" |
"cd", "acde" "abcde", "abbcd" |
"ab" "abc" |
| |
või |
"(ab|cd)+ef" |
"abefg", "cdefg" "ababef", "cdcdef" |
"acef", "efg" "aef", "def" |
\1, \2 |
1., 2. sulu kattumine |
"(.+) \1" |
"a a", "11 12", "55 55", |
"aa", "11 21" |
\w |
w nagu 'word'. Tähed, numbrid, alakriips |
"\w+" |
"abc" "1a" |
".,~@", " " |
\W |
\w vastand. Sama mis [^a-zA-Z0-9_] |
"\W+" |
“.”, “@”, “ “ |
“h”, “ot” |
\s |
s nagu 'space'. Tühik, tabu- laator, reavahetus, alakriips |
"a\sb" |
"a b" "a\tb" |
"ab", "ab" |
\S |
\s vastand |
"a\Sb" |
"ab" "a_b" |
"a b", "a\tb" |
\d |
d nagu 'digit'. Numbrid |
"\d+i" |
"23i" |
"i7" |
\D |
\d vastand |
"\D+i" |
“i”, “.”, “ “ |
“27”, “2i” |
\b |
b ehk 'boundary'. Sõna peab lõppema eeltoodud otsingu- mustriga. |
"d\b” |
"add" |
"adidas" |
\B |
\b vastand |
"d\B" |
"abc" |
"word" |
[^..] |
ei kuulu hulka |
"[^ab]+c" |
"ccc", "dec" |
"abc", "bbc" |
(?=...) |
positiivne lookahead |
"ab(?=cd)" |
"abcd", "abcda" |
"abdd", "abc" |
(?!...) |
negatiivne lookahead |
"ab(?!cd)" |
"abc", "abcccc" |
"oabcd", "abcdd" |
(?<=...) |
posiitvne lookbehind |
"(?<=cd)ok" |
"cdok", "ccdok" |
"cddok" |
(?<!...) |
negatiivne lookbehind |
"(?<!cd)ok" |
"cddok", "ok", "dok" |
"cdok", "ccdok" |