2

関数アドレスについて、gccによって生成されたマップファイルを解析しようとしています。ここ(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で生成されたマップファイルから関数アドレスを取得するための他の実用的なソリューションはありますか?

4

5 に答える 5

10
^\[([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
于 2012-04-19T11:54:31.320 に答える
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% 簡単に読めるわけではありませんが、一致がどのように機能するかについて少し知っていて、インデントが役立つことがわかっている場合に役立ちます。

于 2012-04-19T11:56:25.157 に答える
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. ただし、それらが書かれている方法では、不条理にも一致する可能性がxxxxxxxxxxあります. 0x[0-9A-F]+ここの方が適切でしょう。

(.*?)正規表現は行全体に一致する必要があるため、とにかく貪欲に強制される2番目の正規表現で非貪欲な一致を使用することもあります。

于 2012-04-19T11:56:59.697 に答える
0

これらの正規表現を理解するための貴重なリンクを提供させてください。

これをクリック

最初の正規表現は解析され、次のように説明されます。

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

2番目のものを解析する方法を理解できると思います。

乾杯。

于 2012-04-19T15:48:17.920 に答える
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!  "

2 つ目は次のとおりです。

^            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 "
于 2012-04-19T12:04:27.267 に答える