正則習點 08

2021-08-26 16:37:53 字數 3491 閱讀 1211

在這一節,我們要引入乙個新的概念:環視(lookaround)。

他的定義:不匹配任何字元,只匹配文字中的特定位置(positions)。

環視分兩種:順序環視(lookahead)和逆序環視(lookbehind)。

我們在後面的小段會給出詳細的介紹。

現在我們只需要知道順序環視是從左到右檢視文字;而逆序環視是從右到左檢視文字。

為了理解這個新單詞,我們可以想象她像單詞分界符「\b」、錨點「^」和「$」一樣的工作!但是,比它們更加通用!

2.2.5.1 a few more lookahead examples

乙個不用環視的例子:

s/\bjeffs\b/jeff』s/g

全字匹配「jeffs」,然後,把它替換「jeff』s」。

使用順序環視解決這個問題:

s/\bjeff(?=s\b)/jeff』/g

如圖:首先,匹配「jeff」,然後,嘗試順序環視。

只有當「s\b」在此位置能夠匹配時(也就是』jeff』之後緊跟乙個』s』和乙個單詞分界符)整個表示式才能匹配成功。

也就是「jeff」確定匹配文字,而順序環視只是「選擇」乙個位置。

換成逆序環視的例子:

s/(?<=\bjeff)(?=s\b)/』/g

如圖,我們來總結解決這個問題的幾種方法:

solution

comments

s/\bjeffs\b/jeff』s/g

解決此類問題最容易想到的辦法,未使用環視,正則「占用」整個』jeffs』

s/\b(jeff)(s)\b/$1』$2/g

只增加了變數,沒有多餘的好處

s/\bjeff(?=s\b)/jeff』/g

並沒有占用』s』,除了展示順序環視之外,沒有什麼實用價值。

s/(?<=\bjeff)(?=s\b)/』/g

並沒有「占用」任何文字,同時使用順序環視和逆序環視匹配需要的位置,即撇號插入的位置。非常適用於講解環視。

s/(?=s\b)(?<=\bjeff)/』/g

與上乙個表示式完全相同,只是顛倒了兩個環視結構。因為它並沒有占用任何字元,所以變換順序並沒有影響。

好了,我們在實際中來使用環視。

2.2.5.2 the comma example

大的數值,如果在其間加入逗號,會更容易看懂。

例如,「the us population is 298444215」, 如果使用逗號,「298,444,215」會看起來更加自然。

插入逗號,必須滿足「左邊有數字,右邊數字的個數正好是3的倍數」。

左邊有數字,使用逆序環視:「(?<=\d)」

右邊數字的個數正好是3的倍數,使用順序環視:「(?=(\d\d\d)+$)」

整個正則看起來像:

s/(?<=\d)(?=(\d\d\d)+$)/』/g

我們寫一段**,來驗證它:

#! /usr/bin/perl -w

# mastering regular expressiona: chapter 2 section 2.

# fourth program

$testval = 12345 * 1987;

$testval =~ s/(?<=\d)(?=(\d\d\d)+$)/,/g;

print "this value is $testval decimal.";

執行結果:

this value is 24,529,515 decimal.

2.2.5.3 word boundaries and negative lookaround

我們再增加點難度,如果數字是在整個字串的中間。怎麼辦呢?

$teststr = 「this value is 24529515 decimal.」

這個時候,就不能使用行尾錨點(「$」),而應該換上單詞分界符(「\b」)

修改後的程式如下:

#! /usr/bin/perl -w

# mastering regular expressiona: chapter 2 section 2.

# fourth program

$testval = 12345 * 1987;

$teststr = "this value is $testval decimal.";

$teststr =~ s/(?<=\d)(?=(\d\d\d)+\b)/,/g;

print $teststr;

迄今為止我們用到的順序環視和逆序環視應該被稱作肯定順序環視(positive lookahead)和肯定逆序環視(positive lookbehind)。因為它們成功的條件都是子表示式在這些位置能夠匹配。

那麼有沒有成功的條件是子表示式無法匹配的環視呢?有!

它們是:否定順序環視(negative lookahead)和否定逆序環視(negative lookbehind)。見下表:

four types of lookaround

type

regex

successful if the enclosed subexpression

positive lookbehind

successful if can match to the left

negative lookbehind

successful if can not match to the left

positive lookahead

successful if can match to the right

negative lookahead

successful if can not match to the right

我們再增加點難度,如果數字旁邊有字母,怎麼辦呢?

$teststr = 「this value is 24529515hz decimal.」

這個時候使用否定順序環視會更好!

(?!\d)

更改後的正則如下:

s/(?=\d)(?=(\d\d\d)+(?!\d))/,/g

修改後的程式如下:

#! /usr/bin/perl -w

# mastering regular expressiona: chapter 2 section 2.

# fourth program

$testval = "24529515hz"; # 12345 * 1987

$teststr = "this value is $testval decimal.";

$teststr =~ s/(?<=\d)(?=(\d\d\d)+(?!\d))/,/g;

print $teststr;

正則習點 09

讀取檔案的一段程式 usr bin perl w mastering regular expressiona chapter 2section 2.fiveth program undef textstr print textstr 2.2.6.1 cooking special character...

機器學習08 正則化

一,為什麼要使用正則化 到現在為止,我們已經學習了幾種不同的學習演算法,包括線性回歸和邏輯回歸,它們能夠有效地解決許多問題,但是當將它們應用到某些特定的機器學習應用時,會遇到過度擬合 over fitting 的問題,可能會導致它們效果很差。在這篇博文中,我將為你解釋什麼是過度擬合問題,我們將談論一...

第08天 正則

2.知道程序 執行緒 協程的關係及作用 先程序,然後程序裡面可以有多個執行緒,執行緒裡面可以有多個協程 作用都是可以完成多工的,程序和執行緒完成多工是無序的,協程按照一定順序交替執行 注意點 從資源開銷來說,程序需要使用的資源最多,執行緒次之 512k 協程最少 5k 5.知道正規表示式裡面的.d對...