兩個使用正規表示式來獲取字串中特定子串的方法

2021-05-22 02:09:38 字數 1040 閱讀 8414

前言:獲取字串中的字串的方法有很多,這裡介紹的是使用正規表示式來獲得,都是使用c++的**。

第乙個是使用c中的正規表示式的api。

下面是使用c的api來完成的函式的**:

其中regcomp,regexec都是c下面的函式。

下面舉個例子來說明怎麼使用這個函式。

在linux的/proc目錄下有個有關分割槽資訊的檔案/proc/partitions,它裡面的內容如下(在我們的電腦裡,其他人的會略有不同):

major minor  #blocks  name

8        0  244198584 sda

8        1   29527438 sda1

8        2          1 sda2

8        5   59038843 sda5

8        6  118093783 sda6

8        7   10000431 sda7

8        8     497983 sda8

8        9   27037363 sda9

8       16  156290904 sdb

8       17   53761491 sdb1

8       18  102518797 sdb2

8       32  156290904 sdc

8       33  156288000 sdc1

8       48  156290904 sdd

8       49     313236 sdd1

8       50    3068415 sdd2

現在我們要做的是找出所有的硬碟裝置名,如上面的sda,sdb,sdc和sdd,那麼這個該怎麼做了,看下面的**。

現在說的另外一種方法是使用qt的正規表示式的方法,看下qt下是怎麼使用,同樣是處理上面的/proc/partitions檔案,找出硬碟裝置名。

關於正規表示式和c和qt的正規表示式的api的用法可以google一下,這裡不作介紹了!

用正規表示式擷取字串

region 匹配字元 獲取資訊 匹配字元 獲取資訊 被匹配字串 匹配內容 匹配到的資訊 public static string matchfunction string nowhtml text,string matchname catch return values endregion 使用例...

正規表示式擷取字串操作

string str shdfj3h434343hjhjhgfg 第一步,去除空格trim str.trim 定義乙個空的字串 string str1 null if str null equals str system.out.println str1 前面會生成乙個null這個方法只能吧數字提取...

php使用正規表示式獲取字串中的URL

執行結果為 0 array 0 這裡主要使用到preg match all函式,該函式具體使用方法如下 preg match all 進行全域性正規表示式匹配 語法 int preg match all string pattern,string subject,array matches int ...