我正在尝试解析由gcc生成的映射文件,用于函数地址。有可能solution here(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中获取函数地址?
答案 0 :(得分: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
答案 1 :(得分: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%直接阅读,但如果您对匹配的工作方式有所了解并且发现缩进有用,则可以提供帮助。
答案 2 :(得分: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]+
在这里更合适。
在第二个正则表达式中也使用非贪婪的匹配(.*?)
,因为正则表达式必须与整行相匹配,因此无论如何都会被迫贪婪。
答案 3 :(得分: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 "
答案 4 :(得分: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
我假设您可以弄清楚如何解析第二个。
干杯。