Python 理解正则表达式

Python 理解正则表达式,python,c,regex,gcc,Python,C,Regex,Gcc,我正在尝试解析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) 谁能告诉我重新搜索的目的是什么 是否有其他工作解决方案可以从gcc生成的映射文件

我正在尝试解析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)
谁能告诉我重新搜索的目的是什么


是否有其他工作解决方案可以从gcc生成的映射文件中获取函数地址?

调试Python正则表达式的一种方法是在创建模式对象时使用undocumented re.debug标志

^\[([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
>>> 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>
>>重新导入
>>>重新编译('^\[([0-9]+)\]\s+(.+)\s*$',重新调试)
起初
文字91
子模式1
最大重复次数165535次
在里面
射程(48,57)
文字32
文字93
最大重复次数165535次
在里面
范畴空间
子模式2
最大重复次数165535次
没有吗
最大重复次数0 65535
在里面
范畴空间
最后
这显然不是100%的简单易读,但是如果您对匹配的工作原理有一点了解,并且发现缩进很有帮助,那么它会有所帮助

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
。不过,它们的编写方式也可以与诸如
xxxxxxxxx
之类的荒谬行为相匹配<代码>0x[0-9A-F]+在这里更合适

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

第一个正则表达式是:

^         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!  "
"0xF00 0x1234 [89] Foo"
"78x9 023 Foobar "
第二个是:

^            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
示例:

"[12345] Hello"
"[06 7] \t Foo.Bar!  "
"0xF00 0x1234 [89] Foo"
"78x9 023 Foobar "

让我给你一个无价的链接来找出这些正则表达式

您的第一个正则表达式将被解析并解释为:

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
我想你可以想出如何解析第二个


干杯

(应该注意,末尾的
\s*
总是不匹配,因为
(.+)
中的
+
是贪婪的)