使用正则表达式从具有特殊模式的文本中提取第一行
extract 1st line from a text with special pattern using regexp
我有一个字符串
set text {tools show stats
Resource Manager info at 023 h 06/01/20 09:14:16.105:
Hardware Resource Usage #1, abc iom-32-100g, Pipe #0:
| Total | Allocated | Free
-------------------------------+-----------+-----------+------------
stat vale abc/ghj(0) | 256| 0| 256
stat vale abc/ghj(1) | 256| 0| 256
new Statistic | Count
---------------------------------+-------------
first | 0
seconds| 0
third info | Count
fourth| 0
fifth | 125
Forwarding Table Statistics | Used / Free / Max / Reject
---------------------------------+-----------------------------------
third stat Entries | 2 / 36856 / 36864
Important | 1 / 13133 / 13136 / 0
fifth entry < 56 | 1 / 5743 / 5744 / 0
sixth entry > 5 | 0 / 112 / 112 / 0
Service manager resources
-------------------------
Total count: 2/ 3072
Total info: 2/ 401
ABC Group Statistics | Used / Free / Max
---------------------------------+-----------------------
Entries | 0 / 256 / 256
}
我必须提取
1. Important | 1 / 13133 / 13136 / 0
2. fifth entry < 56 | 1 / 5743 / 5744 / 0
两者都可以单独完成
我在 tcl 中使用以下正则表达式来提取相同的
1. regexp -nocase -line -- {^Important.*} $text match
2. regexp -nocase -line -- {^fifth entry \< 56.*} $text match
它不起作用,有人可以帮忙吗?
regexp -nocase -line -- {^Important.*} $text match
你在 Important
之前有空格,所以你应该像这样将它们添加到你的表达式中:
regexp -nocase -line -- {^ *Important.*} $text match
regexp -nocase -line -- {^fifth entry \< 56.*} $text match
这里也一样(而且<
不需要转义):
regexp -nocase -line -- {^ *fifth entry < 56.*} $text match
匹配前有空格,行是缩进的,所以你需要匹配空格,比如 [[:space:]]*
匹配常规空格和制表符。
或者,如果您不需要在行首匹配,只需删除 ^
。 ^
匹配行首的位置。
从匹配中"exclude"它,捕获你需要的文本,没有使用一对圆括号的初始空格。
您可以使用
regexp -nocase -line -- {^[[:space:]]*(Important.*)} $text - match1
regexp -nocase -line -- {^[[:space:]]*(fifth entry < 56.*)} $text - match2
请注意,如果要匹配文字 <
字符,则不必转义 <
。
详情
^
- 一行的开始(此处,由于 -line
)
[[:space:]]*
- 任意 0 个或多个空格
(Important.*)
- 捕获第 1 组:Important
和除换行符以外的任何 0 个或更多字符。
我有一个字符串
set text {tools show stats
Resource Manager info at 023 h 06/01/20 09:14:16.105:
Hardware Resource Usage #1, abc iom-32-100g, Pipe #0:
| Total | Allocated | Free
-------------------------------+-----------+-----------+------------
stat vale abc/ghj(0) | 256| 0| 256
stat vale abc/ghj(1) | 256| 0| 256
new Statistic | Count
---------------------------------+-------------
first | 0
seconds| 0
third info | Count
fourth| 0
fifth | 125
Forwarding Table Statistics | Used / Free / Max / Reject
---------------------------------+-----------------------------------
third stat Entries | 2 / 36856 / 36864
Important | 1 / 13133 / 13136 / 0
fifth entry < 56 | 1 / 5743 / 5744 / 0
sixth entry > 5 | 0 / 112 / 112 / 0
Service manager resources
-------------------------
Total count: 2/ 3072
Total info: 2/ 401
ABC Group Statistics | Used / Free / Max
---------------------------------+-----------------------
Entries | 0 / 256 / 256
}
我必须提取
1. Important | 1 / 13133 / 13136 / 0
2. fifth entry < 56 | 1 / 5743 / 5744 / 0
两者都可以单独完成 我在 tcl 中使用以下正则表达式来提取相同的
1. regexp -nocase -line -- {^Important.*} $text match
2. regexp -nocase -line -- {^fifth entry \< 56.*} $text match
它不起作用,有人可以帮忙吗?
regexp -nocase -line -- {^Important.*} $text match
你在 Important
之前有空格,所以你应该像这样将它们添加到你的表达式中:
regexp -nocase -line -- {^ *Important.*} $text match
regexp -nocase -line -- {^fifth entry \< 56.*} $text match
这里也一样(而且<
不需要转义):
regexp -nocase -line -- {^ *fifth entry < 56.*} $text match
匹配前有空格,行是缩进的,所以你需要匹配空格,比如 [[:space:]]*
匹配常规空格和制表符。
或者,如果您不需要在行首匹配,只需删除 ^
。 ^
匹配行首的位置。
从匹配中"exclude"它,捕获你需要的文本,没有使用一对圆括号的初始空格。
您可以使用
regexp -nocase -line -- {^[[:space:]]*(Important.*)} $text - match1
regexp -nocase -line -- {^[[:space:]]*(fifth entry < 56.*)} $text - match2
请注意,如果要匹配文字 <
字符,则不必转义 <
。
详情
^
- 一行的开始(此处,由于-line
)[[:space:]]*
- 任意 0 个或多个空格(Important.*)
- 捕获第 1 组:Important
和除换行符以外的任何 0 个或更多字符。