如何捕获在特定字符组合之前找到的两位数字?

时间:2018-02-26 16:16:03

标签: python regex python-3.x

我有以下表格,这些文字来自Telnet套接字。

-> iosFdShow 
 fd name                                     drv
  3 /tyCo/0                                    1 in out err
  4 (socket)                                   7   
  5 (socket)                                   7   
  6 (socket)                                   7   
  7 (socket)                                   7   
  8 (socket)                                   7   
  9 (socket)                                   7   
 10 (socket)                                   7   
 11 logfile                                    2   
 12 /doc0/Log/log.txt                          3   
 13 /doc0/CONFIG_A/System                      3   
 14 /doc0/CONFIG_A/Entity                      3   
 15 /doc0/CONFIG_A/Network                     3   
 16 /doc0/SW_B/fpga.rbf                        3   
 17 (socket)                                   7   
 18 (socket)                                   7   
 19 /tyCo/1                                    1   
 20 (socket)                                   7   
 21 (socket)                                   7   
 22 (socket)                                   7   
 23 (socket)                                   7   
 24 (socket)                                   7   
 25 (socket)                                   7   
 26 (socket)                                   7   
 27 (socket)                                   7   
 30 (dev deleted)tLogin3fedc10               n/a   
 31 (socket)                                   7   
 34 (dev deleted)tLogin4238110               n/a   
 35 (dev deleted)tShellRem73924472           n/a   
 36 (dev deleted)tLogin4238110               n/a   
 39 (dev deleted)tShellRem73924472           n/a   
 40 (dev deleted)tLogin4436b28               n/a   
 41 (dev deleted),)ְ,)׀,)א                n/a   
 42 (dev deleted)tShellRem71667576           n/a   
 43 (dev deleted)tLogin4238110               n/a   
 44 (dev deleted)   S0®ָכr                n/a   
 45 (dev deleted)tLogin4238110               n/a   
 46 (dev deleted)tLogin4355560               n/a   
 47 (dev deleted)tShellRem73924472           n/a   
 48 (dev deleted)tShellRem73924472           n/a   
 49 (dev deleted)tLogin4355560               n/a   
 50 (dev deleted)tLogin4355560               n/a   
 51 (dev deleted)tShellRem73924472           n/a   
 52 (dev deleted)tShellRem73924472           n/a   
 53 (dev deleted)tShellRem73924472           n/a   
 54 (dev deleted)tLogin4355560               n/a   
 55 (dev deleted)tLogin4355560               n/a   
 56 (dev deleted)                            n/a   
 57 (dev deleted)tLogin4355560               n/a   
 58 (dev deleted)tLogin4355560               n/a   
 59 (dev deleted)tLogin4355560               n/a   
 60 (dev deleted)tLogin4355560               n/a   
 61 (dev deleted)tLogin4355560               n/a   
 62 (dev deleted)ַ@e
Jh                n/a   
 63 (dev deleted)tLogin4355560               n/a   
 64 (dev deleted)tLogin4355560               n/a   
 65 (dev deleted)tLogin4355560               n/a   
 66 (dev deleted)כ8                        n/a   
 67 (dev deleted)   S0®ָכr                n/a   
 68 (dev deleted)                            n/a   
 69 (dev deleted)                            n/a   
 70 (dev deleted)                            n/a   
 71 (dev deleted)                            n/a   
 72 (dev deleted)tLogin4355560               n/a   
 73 (dev deleted)tLogin4355560               n/a   
 74 (dev deleted)tLogin4355560               n/a   
 75 (dev deleted)tLogin4355560               n/a   
 76 (dev deleted)tLogin4355560               n/a   
 77 (dev deleted)   S0®ָכr                n/a   
 78 (dev deleted)   S0®ָכr                n/a   
 79 (dev deleted)tLogin4238110               n/a   
 80 (dev deleted)   S0®ָכr                n/a   
 81 (dev deleted)tShellRem77561720           n/a   
 82 (dev deleted)tLogin4355560               n/a   
 83 (dev deleted)tLogin4355560               n/a   
 84 (dev deleted)tShellRem77561720           n/a   
 85 (dev deleted)tShellRem77561720           n/a   
 86 (dev deleted)tLogin4355560               n/a   
 87 (dev deleted)6  ¨2Lץָe               n/a   
 88 (dev deleted)״
ך`‎ןְ                n/a   
 89 (dev deleted)6  ¨2Lץָe               n/a   
 90 (dev deleted)ַ@e
Jh                n/a   
 91 (dev deleted)tShellRem77561720           n/a   
 92 (dev deleted)6  ¨2Lץָe               n/a   
 93 (dev deleted)ַ@e
Jh                n/a   
 94 (dev deleted)                            n/a   
 95 (dev deleted)ַ@e
Jh                n/a   
 96 (dev deleted)                            n/a   
 97 (dev deleted)״
ך`‎ןְ                n/a   
 98 (dev deleted)                            n/a   
 99 (dev deleted)״
ך`‎ןְ                n/a   
value = 100 = 0x64 = 'd'
->

我知道在进入' iosFdShow'命令我将得到一个像结果一样的表,最后是行(字符串)"值= 100 = 0x64 =' d' &#34 ;.我不知道的是这个表中的行数以及该表是否包含其他行(我的框中的调试行)。

如何捕获最后一行的索引?在这种情况下,索引是99

修改

我发现以下情况(最后一行有两个数字使得索引更难捕捉)

-> iosFdShow
 fd name                                     drv
  3 /tyCo/0                                    1 in out err
  4 (socket)                                   7
  5 (socket)                                   7
  6 (socket)                                   7
  7 (socket)                                   7
  8 (socket)                                   7
  9 (socket)                                   7
 10 (socket)                                   7
 11 logfile                                    2
 12 /doc0/Log/log.txt                          3
 13 /doc0/CONFIG_A/System                      3
 14 /doc0/CONFIG_A/Entity                      3
 15 /doc0/CONFIG_A/Network                     3
 16 /doc0/SW_B/fpga.rbf                        3
 17 (socket)                                   7
 18 (socket)                                   7
 19 /tyCo/1                                    1
 20 (socket)                                   7
 21 (socket)                                   7
 22 (socket)                                   7
 23 (socket)                                   7
 24 (socket)                                   7
 25 (socket)                                   7
 26 stdio_pty_16bd7d8.M                        6
 27 (socket)                                   7
 28 (socket)                                   7
 29 stdio_pty_16bd7d8.S                        5
value = 100 = 0x64 = 'd'

在两种情况下,将捕获表中最后一行索引的正则表达式是什么?

1 个答案:

答案 0 :(得分:2)

试试这个正则表达式:

\d+(?=\D*?value = 100 = 0x64 = 'd')

<强> Click for Demo

<强>解释

  • \d+ - 匹配1位以上的数字
  • (?=\D*?value = 100 = 0x64 = 'd') - 积极向前看,以确保当前位置后跟0次出现的非数字,尽可能少,后跟字符串value = 100 = 0x64 = 'd'