2012-04-19 12 views
2

gccによって生成されたマップファイルを関数アドレス用に解析しようとしています。可能なsolution here(python)がありますが、それは私のために働いていません。正規表現を理解する

提供されている解決策を理解しようとしています。それは2つの複雑な正規表現を持っています。

m = re.search('^\[([0-9 ]+)\]\s+(.+)\s*$',line) 
m = re.search('^([0-9A-Fx]+)\s+([0-9A-Fx]+)\s+(\[([ 0-9]+)\]|\w+)\s+(.*?)\s*$', line) 

REは何を探していますか?

gccが作成したマップファイルから機能アドレスを取得する他の解決策はありますか?

答えて

9
^\[([0-9 ]+)\]\s+(.+)\s*$ 

^     start of the line 
\[     literal [ 
([0-9 ]+)   group of 0-9 or space, one or more times 
\]     literal ] 
\s+    one or more spaces 
(.+)    group of anything one or moretimes 
\s*    zero or more spaces 
$     end of line 


eg: "[5 5 5] blah" 

gives: 
    group1 = "5 5 5" 
    group2 = blah 

^([0-9A-Fx]+)\s+([0-9A-Fx]+)\s+(\[([ 0-9]+)\]|\w+)\s+(.*?)\s*$ 

^     start of line 
([0-9A-Fx]+)  group of chars one or more times 
\s+    one or more spaces 
([0-9A-Fx]+)  group of chars one or more times 
\s+    one or more spaces 
(
    \[    literal [ 
    ([ 0-9]+)  group of char 1 or more times 
    \]    literal [ 
    |    or 
    \w+   word char, one or more times 
) 
\s+    one or more spaces 
(.*?)    any char zero or more times, non greedy 
\s*    zero or more spaces 
$     end of line 
+2

(最後に '\ s *'は '(。+)'の '+ 'は貪欲であるので)何も一致しないことに注意してください。 – huon

6

Python正規表現をデバッグする方法の1つは、パターンオブジェクトを作成するときに文書化されていないre.DEBUGフラグを使用することです。

>>> import re 
>>> re.compile('^\[([0-9 ]+)\]\s+(.+)\s*$', re.DEBUG) 
at at_beginning 
literal 91 
subpattern 1 
    max_repeat 1 65535 
    in 
     range (48, 57) 
     literal 32 
literal 93 
max_repeat 1 65535 
    in 
    category category_space 
subpattern 2 
    max_repeat 1 65535 
    any None 
max_repeat 0 65535 
    in 
    category category_space 
at at_end 
<_sre.SRE_Pattern object at 0x01CE8950> 

それは明らかに読むことが100%簡単ではないのですが、それはあなたが作品を一致する方法について少し知っていれば助けとインデントが参考に見つけることができます。

+1

+1 - それは便利なトリックです。 –

1
pattern1 = re.compile (
r""" 
^      # start of string 
\[      # literal [ 
([0-9 ]+)    # Collection of numbers and spaces 
\]      # literal ] 
\s+      # whitespace 
(.+)     # any string of at least one character 
\s*      # possible whitespace 
$      # end of string 
""", re.VERBOSE) 

pattern2 = re.compile (
r""" 
^      # Start of string 
([0-9A-Fx]+)   # Collection of hexadecimal digits or 'x' 
\s+      # Whitespace 
([0-9A-Fx]+)   # Collection of hexadecimal digits or 'x' 
\s+      # Whitespace 
(\[([ 0-9]+)\]|\w+)  # A collection of numbers, or space, inside [] brackets 
\s+      # Whitespace 
(.*?)     # Any string 
\s*      # Possible whitespace 
$      # End of string 
""", re.VERBOSE) 

これは実際にはかなり正式に書かれています。

([0-9A-Fx]+)サブグループは、実際には、0x1234DEADBEEFのような16進数と一致することを意味します。彼らが書かれている方法は、しかし、彼らはxxxxxxxxxxのような不条理にもマッチする可能性があります。ここでは0x[0-9A-F]+がより適切でしょう。

2番目の正規表現には貪欲でない一致を使用することもあります。この正規表現は、行全体と一致する必要があるため、貪欲にする必要があります。

0

最初のものである:

^   start of string 
\[  a '[' 
([0-9 ]+) one or more digits and spaces 
\]  a ']' 
\s+  whitespace 
(.+)  anything 
\s*  optional whitespace 
$   end of string 

例:

"[12345] Hello" 
"[06 7] \t Foo.Bar! " 

第二つある:

^   start of string 
([0-9A-Fx]+) hex digits and x 
\s+   whitespace 
([0-9A-Fx]+) hex digits and x 
\s+   whitespace 
(   either: 
\[    a '[' 
([ 0-9]+)  digits and spaces 
\]    a ']' 
|   or: 
\w+   a word 
)   end group 
\s+   whitespace 
(.*?)  optional anything (non-greedy) 
\s*   optional whitespace 
$   end string 

例:

"0xF00 0x1234 [89] Foo" 
"78x9 023 Foobar " 
0

これらの正規表現を理解する貴重なリンクを教えてください。私はあなたが解析された秒1を取得する方法を見つけ出すことができます仮定

NODE      EXPLANATION 
-------------------------------------------------------------------------------- 
^      the beginning of the string 
-------------------------------------------------------------------------------- 
    \[      '[' 
-------------------------------------------------------------------------------- 
    (      group and capture to \1: 
-------------------------------------------------------------------------------- 
    [0-9 ]+     any character of: '0' to '9', ' ' (1 or 
          more times (matching the most amount 
          possible)) 
-------------------------------------------------------------------------------- 
)      end of \1 
-------------------------------------------------------------------------------- 
    \]      ']' 
-------------------------------------------------------------------------------- 
    \s+      whitespace (\n, \r, \t, \f, and " ") (1 or 
          more times (matching the most amount 
          possible)) 
-------------------------------------------------------------------------------- 
    (      group and capture to \2: 
-------------------------------------------------------------------------------- 
    .+      any character except \n (1 or more times 
          (matching the most amount possible)) 
-------------------------------------------------------------------------------- 
)      end of \2 
-------------------------------------------------------------------------------- 
    \s*      whitespace (\n, \r, \t, \f, and " ") (0 or 
          more times (matching the most amount 
          possible)) 
-------------------------------------------------------------------------------- 
    $      before an optional \n, and the end of the 
          string 

:として

Click on this

あなたは最初の正規表現を解析し、説明します。

乾杯。