簡體   English   中英

用正則表達式解析C語言文件

[英]parsing a file in C language with regex

我正在嘗試解析這樣的長文件(Linux 中命令 play 的輸出):

 File :1.mp3
In:0.00% 00:00:00.00 [00:03:14.51] Out:0     [      |      ]  


In:0.19% 00:00:00.37 [00:03:14.14] Out:16.4k [      |      ]          
In:0.29% 00:00:00.56 [00:03:13.95] Out:24.6k [======|======]          
In:0.33% 00:00:00.65 [00:03:13.86] Out:28.7k [ =====|===== ]   
In:0.43% 00:00:00.84 [00:03:13.67] Out:36.9k [ =====|===== ]   
In:0.53% 00:00:01.02 [00:03:13.49] Out:45.1k [ -====|===== ]    
In:0.62% 00:00:01.21 [00:03:13.30] Out:53.2k [ =====|===== ]     
In:0.72% 00:00:01.39 [00:03:13.11] Out:61.4k [-=====|======]    
In:0.81% 00:00:01.58 [00:03:12.93] Out:69.6k [-=====|=====-]   
In:0.91% 00:00:01.76 [00:03:12.74] Out:77.8k [-=====|=====-]     
In:0.96% 00:00:01.86 [00:03:12.65] Out:81.9k [ =====|===== ]    

等等
我想解析百分比數字。 我怎么能不將文件保存到(因為太大~100KB)一個字符串中。 我想用這個正則表達式: "In:(\\d{1,2}\\.\\d{2})"怎么做?

試試這個正則表達式:

/^In:([0-9]{1,3}\.[0-9]{1,2})\%/gm

解釋:

/
 ^                                 Matches start of string.
  In:                              Matches "In:".
     (                      )      Groups percentage (excl. sign).
      [0-9]{1,3}                   Matches 1-3 (incl.) numbers.
                \.                 Matches a dot.
                  [0-9]{1,2}       Matches 1-2 (incl.) numbers.
                             \%    Matches a percent sign.
                               /gm Allows multiple matches and makes ^ match beginning of line (not beginning of string), respectively.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM