Linux with Operating System Concepts



Download 5,65 Mb.
Pdf ko'rish
bet80/254
Sana22.07.2022
Hajmi5,65 Mb.
#840170
1   ...   76   77   78   79   80   81   82   83   ...   254
Bog'liq
Linux-with-Operating-System-Concepts-Fox-Richard-CRC-Press-2014

substring
and 
string
. We will consider a string to be a line of text. 
A substring is any (0 or more) consecutive characters of a string. The line of text that makes 
up our string will typically be one line of a textfile. The line ends with an end-of-line char-
acter, often represented as 
\n
. We write a regular expression to match any portion of that 
string. If there is a match, then we match the string on the line. If we want our expression 
to match the entire line (or string), we have to specify this in our expression.
By this new definition, a regular expression that matches the empty string (no charac-
ters at all) will match that line. For instance, the expression 
a*
will match 0 or more a’s, so 
if there are no a’s on the line, it still matches.
In fact, the regular expression 
a*
would match any line. This is why it is important to 
control 
where
a regular expression is allowed to match within a line. This leads us to the 
next two metacharacters: 
^
and 
$
. The 
^
metacharacter indicates that the regular expres-
sion can only match from the start of the string (the beginning of the line). The 
$
metacha-
racter indicates that the regular expression can only match at the end of the string.
If, for instance, we have the expression 
^b?.t
+
(the earlier expression with a ^ added to 
the front), then this will match a string that 
starts with
0 or 1 b followed by anything fol-
lowed by 1 or more t’s. This will not match the earlier string of 
bbbbbbbattttttt
. Why 
not? The 
^b
forces the regular expression to begin matching this string from the begin-
ning. Therefore, 
b?.
will match the first b (0 or 1 b) and the second b (the period matches 
any single character) but now the regular expression expects the remainder of the string to 
be one or more t’s and since we have another b, there is no match. Refer back to Figure 6.1 
where you can see that the 
t
+
portion of the regex fails to match.
To obtain a complete match of a regular expression to a string, use both 
^
and 
$
. For 
instance, 
^b?.t
+
$
will match any string that starts with 0 or 1 b, has any character, and 
bbbbbbbattttttt
X
^b?.t+
b?. t+
FIGURE 6.1 
Controlling where an expression matches.


Regular Expressions

207
ends with 1 or more t’s. So this regex will match the following strings: 
battttttt,
bbtttttt,
and 
attttttt
(in this case there are 0 b’s).
Let us examine some more examples using 
^
and/or 
$
. We start with the regex 
^0a*1
+

This regex says that to match a string, it must start with a 0. This is followed by 0 or more 
a’s followed by 1 or more 1’s. This string will therefore match any of 
0a1

0aaaa1111

011111,
and 
01
. It can also match strings where there are characters after the 1 because 
we are not restricting this regex to “end the string” using 
$
. So it will also match 
0a1a
and 
011110
. What it cannot match are strings like 
0a
(there must be at least one 1), 
0000aaaa1111
(after the single 0 at the start). We must see a’s if any and at least one 1 
before any other characters or 
aaaa1111
because there must be a 0 to start.
Now consider the variation 
^0a*1
+
$
. With the closing 
$
, we are stating that a string 
must not contain any characters after the 1 (or 1’s). Thus, 
0aaaa1111
and 
01
will match 
but 
0a1a
and 
011110
will not.
One last example to finish this subsection is the regex ^$. Since the ^ indicates that the 
regex must match at the start of the string and the $ indicates that the regex must match 
at the end of the string, this regex must match the full string. But as there are no literal 
characters or metacharacters between the two, this says “match any string that has nothing 
in it” which is known as the 

Download 5,65 Mb.

Do'stlaringiz bilan baham:
1   ...   76   77   78   79   80   81   82   83   ...   254




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish