2

我正在尝试解析由 gcc 生成的映射文件以获取函数地址。这里有一个可能的 解决方案(python),但它不适合我。

我试图了解所提供的解决方案。它有两个复杂的正则表达式..

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 生成的 mapfile 获取函数地址?

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 正则表达式的一种方法是在创建模式对象时使用未记录的 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]+在这里会更合适。

在第二个正则表达式中还使用了非贪婪匹配(.*?),无论如何这将被迫贪婪,因为正则表达式必须匹配整行。

于 2012-04-19T11:56:59.697 回答
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 "
于 2012-04-19T12:04:27.267 回答
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

我假设您可以弄清楚如何解析第二个。

干杯。

于 2012-04-19T15:48:17.920 回答