15 Aralık 2016 Perşembe

Regular Expression Basics - 1

In this post I wanna share some basic regular expression rules. But first I need to write down the metacharacters. Metacharacters are those characters which have a special meaning in regular expressions. i.e. "-" and "[" handled differently in regular expressions. Metacharcters are are as follows:

Dot (.) matches anything, except for the line breaks.

1a5, 41e5y, 1x5o, 89xy1&5P55 are all the same


If you need to include dot (.) in your search than you have to put a backslash (\) before the dot (.)
According to the above expression it matches only the characters "3" and "9" in between they have a "."
93.96, a3.9t, 13.9s, x3.9t

If you want to search more characters which you don't want to type one-by-one than there is a solution which is called quantifiers.
For example if you want to search 10 "b"s than you have to type either /bbbbbbbbbb/g or /b{10}/g. If you're gonna search 10 "b"s than that's may be okay but if you're gonna search 100 "b"s it doesn't seem so straightforward.
If you want to search at least 6 "b"s than you have to put a comma (,) after the number which is between paranthesis. /b{6,}/g won't catch 5 "b"s it will only catch "b"s which are 6 or more.
If you want at least 7 "b"s but at most 10 "b"s then you have to type two numbers in between the paranthesis. i.e. /b{7,10}/g

This regexp matches either "g", or "y" or "b". This regexp finds only "1" occurence of one of these letters.

If you put a "+" after the square bracket then it will find any occurences of "g" or "y" or "b".
ggyybb, gyyyybbb, bbb, yy, bgyyy, yyggaaa, lkbgyata, 12hytbyggg90g0b

If you want to catch all the letters of the English alphabet in the regexp than you can define a range. The regular expression above catches every letter of the English alphabet. But it doesn't catch any numbers or symbols regardless of their occurences.
abjuyr, amnxc5, 77yhbbd76dg76, xbn8dmmdn, %!98hnsb!!mmcasd98lk=

The expression above detects all the letters of the English alphabet and all the letters regardless of their occurences.
11009hsndh%88uj, 56+67=123=xyz, $var=99

/[a-zA-Z0-9_]+/g equals to /\w+/g
There are some shortcuts of the whole English alphabet and whole numbers. i.e. the regular expressions /[a-z0-9_]+/g and /\w+/g are the same. Both find the letters (small or capital) and numbers including the underscore (_) regardless of their occurences.

/[0-9]+/g equals to /\d+/g
If you want to match only numbers then you have to use the one of the above regular expressions.


You can negate the classes with the "^" character. With the above regular expression you will catch the characters other than [t-z]. namely the regular expressin will match the small letters a, b, c, d, e, f, g, h, i, j, k, l and all the capital letters from A to Z and also the numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 and also symbols like %, & etc.

whatta99yathink88? iaint77do21ANYTHING%% HappyNewYear-In-2017!!!


/^\w+/g = /\W+/g
The above regualar expression above matches anything that's not the letter or number or underscore (_)



/^\d+/g = /\D+/g

The above regular expression above matches anything that is not a number.

14mfly1ngt0C4n4ry15l4nd 4w0rldw1th0utr3g3xp5??!!

Hiç yorum yok:

Yorum Gönder