About Regex (Regular Expressions) commands and their use cases Gökhan Kandemir‘s excellent write-up that I came across, and I wanted to share it with you. Also, KablosuzKedi On the YouTube channel, you can find quite comprehensive lessons and explanations on many topics.
Anyone interested in software has surely needed Regex at some point. Thanks again for such useful information — at the very least, it’s great to have a reference like this at hand when you need it.
Regular Expressions | Regex | Regular Expressions
Yesss, here we are at the well-known topic. Regex first of all gives us permission to select a character. This character or group of characters changes entirely depending on your point of view.
Flags
/g match All /m multiline /i case sensitive /u unicodeNicelikler / Notasyonlar
### + (one or more) Selects one or more characters that match the condition as a single expression.
Aloooo burada ne oluyor acaba?in a sentence like
o+yaparsak bize
Aloooo burada ne oluyor, I wonder?
it will return the expressions.
? Opsiyonel karakter
Used to mark optional characters
Aloooo burada ne oluyor acaba?in a sentence like
o+r?yaparsak bize
Aloooo burada ne oluyor acaba?
expressions will be returned. Here o+ because we said oooo it gave us the entire match as
* (zero or more) | ? and + Union
+ ve ? is a combination. Optionally, it takes the possible match as a combination again.
Aloooo burada ne oluyor acaba?in a sentence like
o+r*yaparsak bize
Aloooo burada ne oluyor acaba?
expressions will be returned. Because here * due to the nature of the character r is positioned optionally. Even if it is present, o ve r it will treat it as a combination.
### . (dot) Notation represents any character except a newline before or after it.
Aloooo burada ne oluyor acaba? Yeni video geldi. I think it's a nice video olmuşd..yaparsak bize
Aloooo burada ne oluyor acaba? Yeni video geldi. I think it’s a nice video became
expression will be returned. If one more of this *(.) nokta koyarsak
d...like this, this time selection
Helloooo hereda ne oluyor acaba? Yeni video geldi. I think it’s a nice video has become
will return the expression.
Fakat Nokta karakterini aramak istersek bu durumda;
. ile escape yaparak nokta karakterini arayabiliriz.
w
Selects all non-space characters one by one.
W negatif versiyon (w)
It is the opposite of w. It selects only spaces one by one.
s
Selects spaces
S Negatif versiyon (s)
It is the opposite of s. It selects all characters except spaces one by one.
d Numbers
Selects numbers
D Non-numbers
Selects non-numeric characters.
Range specification
### {x,y} x selects a word group consisting of characters. y does not have to be specified. If it is not specified, it is interpreted as x or more.
w{4,}4 karakterden fazla olan kelimeleri sec.
## Character Grouping [fc]at Beginning f ya da starting with c “at” words ending with. We can also use character grouping to specify a range. For example, a ile z like all characters between.
fat cat hat tat nat Fat Cat 4at 5at[a-tA-T0-5]atRange Specification
[a-z] [A-Z] [a-zA-Z] [0-9] [a-f]gibi..
[a-zA-Z]atGrouping Operation (…)
It allows capturing more than just one character as a group; in (t|T)he, the first character can be t or T, and it ends with he..
The fat cat ran down the street.(t|e|r){2,3}.Selects word groups of at least 2 and at most 3 characters that contain t or e or r and end with “.”.
This gives us eet. ifadesini verecektir.
rerere rarara something, championburadan re{2,3} üzerinden ilerleyeceğiz.
(re|ra){2,3}Select word groups that contain at least 2 and at most 3 “re”
rere gibi ya da rerere### ^ Matching the character at the beginning of a line
I am walking alone on the street. the street isn't even aware of it.^[Ss]okakSelect character groups that start with T or t at the beginning of a line and end with he!
### $ Satır sonundaki karakteri belirleme
.$expressions ending with a period.
## Lookahead
With the characters or character groups we specify devam eden yada devam etmeyen allows us to select expressions
ifade(lookahead)can be thought of as follows. There are 2 different methods.
### Pozitif Lookahead | Seç – ?=
Negatif Lookahed’in tam tersidir. Continue with the character we specified eden selects expressions. For example, starting with q and continuing with u etmeyen if we want to select an expression
quantity and qrcode is really usefulq(?=u)[a-zA-Z0-0]+an expression can be written as follows. The explanation here is q starting with u means select the word group that starts with and continues with.
Again, based on the same text u ile devam eden karakterleri Select demek istersek
.(?=u)bu bize q ve space ve f will select the characters.
A different example
The fat cat ran down the street. rereifadesinden at ile devam eden if you want to say select the characters
.(?=at)bu ifade bize f ve c returns the characters. Because in the sentence at ile devam eden sadece fat ve cat there is
here it captures f and c.
Negative Lookahead | Not Selecting – ?!
Selects expressions that do not continue with the character we specified. For example, if we want to select an expression that starts with q but does not continue with u
quantity and qrcode is really usefulq(?!u)[a-zA-Z0-9]+here, starting with q and continuing with u devam etmeyen selects the word group. With [a-zA-Z0-9] we made it decide to select any character it can go with, and this will give us qrcode the word back.
q(?!r)[a-zA-Z0-9]+If we replace this expression u yerine r with it, that is, starting with q and continuing with r etmeyen and say select the word group, in this case quantity it will be selected.
Again, based on the same text u ile devam eden karakterleri selection demek istersek
.(?!u)bu bize q ve space ve f will select all other characters except.
A different example
The fat cat ran down the streetkelimesinden at ile devam etmeyen if you mean select all characters
.(?!at)burada f ve c except all of them selects one by one because the dot (.) has been used as the selector.
Look Behind
with the character or character groups we specified beforehand devam eden yada devam etmeyen allows us to select expressions
(lookabehind)ifade### Pozitif Look Behind | Seç – ?<=
The expressions we will choose preceding karakter ve karakter grubunun whether it is present or not checks. Thus, if beforehand if this is present, select that allows for a selection condition such as
quantity and qrcode is really unuseful but this is an unethicalin a sentence like this un let’s select the characters that start with.
(?<=un).quantity and qrcode is really unuseful but this is an unethical
characters will be returned. Because before it un characters are present.
A different example
quantity and qrcode is really unuseful but this is an unethical. The man was born in early age. Also the man is really oldthe words we want to capture in the sentence punisher ve man if they had words. Bunun before The var mı yok mu diye kontrol edebilirdik. Another perspective was: how could we get me words that start with The or the? olabilir. For this
(?<=[tT]he )[a-zA-Z]+expression will be sufficient for us. Because The or the are two different words we want to select, T or t should be optional here. This expression gives us the following punisher ve man kelimelerini verecektir.
Negatif Look Behind | Seçme – <?!
It is the exact opposite of a positive lookbehind. Positive lookbehind when selecting, if the character or character group preceding the negative lookbehind is present, the following expression does not select.
quantity and qrcode is really unuseful but this is an unethicalin a sentence like this un characters that start with let’s not select.
(?<!un).quantity and qrcode is really unuseful but this is an unethical
will return all characters except these characters. Because before it un characters are present.
A different example
quantity and qrcode is really unuseful but this is an unethical. The man was born in early age. Also the man is really oldin the sentence before The veya the if we want to say select all characters that are not
(?<![Tt]he ).the expression will be sufficient for us. Because The or the are two different words we want to choose, T or t should be optional here. Bu ifade bize;
quantity and qrcode is really unuseful but this is an unethical. The punisher was born in early age. Also the man is really old
ifadesini verir. Because characters that continue with The or the p ve m characters. It will select all except these characters.
Examples
### Selecting a Phone Number
1234567890 123-456-7890 123 456 7890 (123) 456-7890 +1 123 456 7890d{3}[ -]?d{3}[ -]?d{4}(?<areacode>d{3})[ -]?(?<inital>d{3})[ -]?(?<deneme>d{4})(?(?<areacode>d{3}))?[) -]?(?<inital>d{3})[ -]?(?<deneme>d{4})(+d{1}[ -])?(?(?<areacode>d{3}))?[ -]?(?<inital>d{3})[ -]?(?<deneme>d{4})(?:(+d{1})[ -])?(?(?<areacode>d{3}))?[ -]?(?<inital>d{3})[ -]?(?<deneme>d{4})(?:(+d{1})[ -])?(?(?<areacode>d{3}))?[ -]?(?<inital>d{3})[ -]?(?<deneme>d{4})(?<area>(+d{1,}))[ -]?(?(?<operator>d{3}))?[ -]?(?<main>d{3})[ -]?(?<number>d{4})((?<area>+d{1,2})[ -])?(?(?<operator>d{3}))?[ -]?(?<main>d{3})[ -]?(?<number>d{4})Done in the video
(?<areaCode>+d{2})?[ ]?(?(?<operator>d{3}))?[ -]?(?<main>d{3})[ -]?(?<number>d{4})Tarih Validasyonu
14/02/2018 14-02-2018 14.02.2018 14.02.18(?<day>([0-9]{2}))([/-.])(?<month>([0-9]{2}))([/-.])(?<year>([0-9]{2,4}))Done in the video
(?<day>d{2})[/-.](?<month>d{2})[/-.](?<year>d{2,4})HOMEWORK
2018/02/14 2018-02-14 2018.02.14 18.02.14Getting information from [url~title]
[https://www.videosinif.com~videosinif] [https://www.kablosuzkedi.com,kablosuzkedi] [https://www.youtube.com/kablosuzkedi|kablosuzkedi youtube kanalı]First, let’s get the URL part
(?<=[)(.*)(?=~)pozitif look behind ve pozitif look ahead
(?<url>((?<=[)(.*)(?=~)))Let’s separate the URL by grouping it
(?<title>(?<=~)(.*)(?=]))title bilgisini de gruplayarak alabiliriz.
(?<url>((?<=[)(.*)(?=[~|,||])))[~,|](?<title>(?<=[~|,||])(.*)(?=]))(?<url>(?<=[)(.*)(?=[~,|]))[~|,||](?<title>(?<=[~|,||])(.*)(?=]))?Done in the video
(?<=[)(?<url>.*)(?=[~,|])[~,|](?<=[~,|])(?<title>.*)(?=])To allow both of these expressions to be collected in the same groups, we add a ~ between them and select the entire expression.
### Getting a Key: value Pair
Name: Gokhan LastName: Kandemir Address: Adana Age: 33 Married: Yes(?<fieldName>^[a-zA-Z]+): (?<value>[a-zA-Z0-9]+)### Getting Links from a Web Page
<a(s+)href="(?<url>([^"]*))"<img(s)+src='[^']*'[^’] => Select all characters that are not ‘
### <body>…</body> to get the content inside it.
<body[^>]*>([w|W]*)</body>### Email Validasyonu
I received a nice email from gokhan@gkandemir.com. So what do we have against .com email addresses, bro? Along with that, I also have another email address, delphixdfd@gmail.com, that I got during my teenage years :D([a-zA-Z0-9])+@([a-zA-Z0-9])+.[a-zA-Z]{2,}Done in the video.
w+@w+.[a-zA-Z]{2,}### URL Validasyonu
burada bir ton web sayfası var. https://www.google.com bunlardan bir tanesi. Neden olduğunu bilmiyorum ama http://www.test123.space de bunlardan bir tanesi. Oldukça güzel bir web sayfası daha var burada www.kablosuzkedi.com uzun zamandan beri güncellenmemiş fakat yine de bilgiler işe yarayabilir. Fakat video izlemek isterseniz youtube.com da buna uyan diğer bir güzel web sayfası(https?:?//)?(www)?.?[a-zA-Z0-9]+.[a-zA-Z]{2,}The example we did in the video
(https?://)?(www.)?([a-zA-Z0-9]+)(.[a-zA-Z]{2,})Parsing Hashtags
Regex için video hazırlıyorum. #Regex ile çözümlemek için Bana uğraştığınız merak ettiğiniz metinleri yazabilir misiniz? Mesela #Web sayfasındaki <body></body> #tag 'leri arasındaki bilgileri almak gibi. Bu #kolay tabi :) #Derdımianlatabilmişimdirumarim :) #360dayscleancode#[a-zA-Z0-9işüğçöı]+Youtube, Vimeo, İzlesene Video URL Parsing
data-config-url="https://player.vimeo.com/video/488734703/config?autopause=1&autoplay=1&byline=0&collections=1&context=Vimeo%5CController%5CClipController.main&default_to_hd=1&outro=nothing&portrait=0&share=1&title=0&watch_trailer=0&s=8be48fe12cfacadb79085e9c2acbd6568c1fb641_1609112069" data-fallback-url="//player.vimeo.com/video/488734703/fallback?js" ZeroMQ nedir isimli video şu an yayında! https://www.youtube.com/watch?v=YAYp7hbOu7o Pentagram'ın güzel bir şarkısı güzel bir şarkı gibi sanki ama eski tadını vermiyor https://www.izlesene.com/video/pentagram-bu-duzen-yikilsin/10523935(https://)(www.)?(?<vimeo>(player.vimeo.com/video/[0-9]+/)?)(?<youtube>youtube.com/watch?v=[a-zA-Z0-9]+)?(?<izlesene>(izlesene.com/video/[a-zA-Z0-9/-]+))?The code we wrote with JavaScipt
JavaScript ile E-mail Validasyonu
const email_regex = /w+@w+.[a-zA-Z]{2,}/g; if (email_regex.test("gokhan@gkandemir.com")) { alert("Başarılı"); } else { alert("Başarısız") }JavaScript ile Hashtag Listesini Almak
const regex = /#[a-zA-Z0-9şığüçö]+/gm; const str = `Regex için video hazırlıyorum. #Regex ile çözümlemek için Bana uğraştığınız merak ettiğiniz metinleri yazabilir misiniz? Mesela #Web sayfasındaki <body></body> #tag 'leri arasındaki bilgileri almak gibi. Bu #kolay tabi :) #Derdimianlatabilmişimdirumarım :) #360dayscleancode`; // console.log(str.match(regex)); str.match(regex).forEach(h => console.log(h));Son olarak isterseniz bu via the link You can test yourself by reviewing the Regex examples. I can say it’s quite helpful 🙂
There is also a video explanation below.
[su_youtube url=”https://www.youtube.com/watch?v=bF_zEzFQZuA&t=442s” title=”Regex”]
Kaynak: kablosuzkedi | Gökhan Kandemir

Share your experience
Got a question, a different solution or a note to add?
Log in or join us to write a comment.
Log in ↗︎Sign up ↗︎