在文本字符串中查找Python的值

huangapple go评论114阅读模式
英文:

Python find value in text string

问题

我正在使用Python编写一个网络自动化脚本,遇到了一个我不知道如何解决的问题。我有以下输出:

  1. CPE-MGT {
  2. instance-type virtual-router;
  3. interface ge-0/0/0.0;
  4. routing-options {
  5. static {
  6. route 192.168.253.115/32 next-hop 192.168.100.1;
  7. route 0.0.0.0/0 next-hop 192.168.100.1;
  8. }
  9. }
  10. }
  11. DATA {
  12. instance-type virtual-router;
  13. interface ge-0/0/1.0;
  14. }
  15. MGMT_BK {
  16. instance-type virtual-router;
  17. interface ge-0/0/2.0;
  18. routing-options {
  19. static {
  20. route 192.168.253.115/32 next-hop 192.168.100.1;
  21. route 0.0.0.0/0 next-hop 192.168.100.1;
  22. }
  23. }
  24. }

我需要获取特定主键接口的名称所在的位置;也许如果我举个例子会更清楚。

我有一个正则表达式,它查找文本并发现ge-0/0/0.0现在在文本中;我不知道的是如何获取CPE-MGT。

  1. cmd = net_connect.send_command(
  2. juniper_cmds["sh_routing_instance"])
  3. logger.debug(f'{ip_dict[config_dict["csv_ip_column"]][ips]}:设备路由实例 {cmd}')
  4. regex_routing_instance = re.compile(f'{interface[0]}')
  5. routing_instance = regex_routing_instance.findall(cmd)

希望这对你有帮助。

英文:

I am building a network automation script in python, and I got into a problem that I don't know how to resolve. I have the following output

  1. CPE-MGT {
  2. instance-type virtual-router;
  3. interface ge-0/0/0.0;
  4. routing-options {
  5. static {
  6. route 192.168.253.115/32 next-hop 192.168.100.1;
  7. route 0.0.0.0/0 next-hop 192.168.100.1;
  8. }
  9. }
  10. }
  11. DATA {
  12. instance-type virtual-router;
  13. interface ge-0/0/1.0;
  14. }
  15. MGMT_BK {
  16. instance-type virtual-router;
  17. interface ge-0/0/2.0;
  18. routing-options {
  19. static {
  20. route 192.168.253.115/32 next-hop 192.168.100.1;
  21. route 0.0.0.0/0 next-hop 192.168.100.1;
  22. }
  23. }
  24. }

I need to obtain the name of there I find the name of specific primary key interfaces; perhaps it will be clearer if I give an example.

I have a regex that looks into the text and finds that ge-0/0/0.0 is in the text now; what I don't know is how to obtain CPE-MGT.

  1. cmd = net_connect.send_command(
  2. juniper_cmds["sh_routing_instance"])
  3. logger.debug(f'{ip_dict[config_dict["csv_ip_colum"]][ips]}:Device routing instances {cmd}')
  4. regex_routing_instance = re.compile(f'{interface[0]}')
  5. routing_instance = regex_routing_instance.findall(cmd)```
  6. </details>
  7. # 答案1
  8. **得分**: 2
  9. 你可以使用两个模式和 `regex` 模块来实现递归:
  10. ```python
  11. import regex as re
  12. data = &quot;&quot;&quot;
  13. CPE-MGT {
  14. instance-type virtual-router;
  15. interface ge-0/0/0.0;
  16. routing-options {
  17. static {
  18. route 192.168.253.115/32 next-hop 192.168.100.1;
  19. route 0.0.0.0/0 next-hop 192.168.100.1;
  20. }
  21. }
  22. }
  23. DATA {
  24. instance-type virtual-router;
  25. interface ge-0/0/1.0;
  26. }
  27. MGMT_BK {
  28. instance-type virtual-router;
  29. interface ge-0/0/2.0;
  30. routing-options {
  31. static {
  32. route 192.168.253.115/32 next-hop 192.168.100.1;
  33. route 0.0.0.0/0 next-hop 192.168.100.1;
  34. }
  35. }
  36. }
  37. &quot;&quot;&quot;
  38. rx_block = re.compile(r&quot;&quot;&quot;
  39. ^(?P&lt;name&gt;[A-Z_-]+)\s+
  40. (?P&lt;body&gt;\{
  41. (?:(?:[^{}]+)|(?2))+
  42. \})
  43. &quot;&quot;&quot;, re.M | re.X)
  44. rx_inner = re.compile(r&quot;interface ge-0/0/0.0;&quot;)
  45. for block in rx_block.finditer(data):
  46. m = rx_inner.search(block.group(&#39;body&#39;))
  47. if m:
  48. print(block.group(&#39;name&#39;))

这将产生以下结果:

  1. CPE-MGT

这里有两点需要注意:你实际上不需要使用正则表达式进行内部搜索,因为它是一个静态字符串,另外,可能应该使用解析器解决方案。你可以在 regex101.com 上查看该表达式的演示

英文:

You could use two patterns and the regex module which offers recursion:

  1. import regex as re
  2. data = &quot;&quot;&quot;
  3. CPE-MGT {
  4. instance-type virtual-router;
  5. interface ge-0/0/0.0;
  6. routing-options {
  7. static {
  8. route 192.168.253.115/32 next-hop 192.168.100.1;
  9. route 0.0.0.0/0 next-hop 192.168.100.1;
  10. }
  11. }
  12. }
  13. DATA {
  14. instance-type virtual-router;
  15. interface ge-0/0/1.0;
  16. }
  17. MGMT_BK {
  18. instance-type virtual-router;
  19. interface ge-0/0/2.0;
  20. routing-options {
  21. static {
  22. route 192.168.253.115/32 next-hop 192.168.100.1;
  23. route 0.0.0.0/0 next-hop 192.168.100.1;
  24. }
  25. }
  26. }
  27. &quot;&quot;&quot;
  28. rx_block = re.compile(r&quot;&quot;&quot;
  29. ^(?P&lt;name&gt;[A-Z_-]+)\s+
  30. (?P&lt;body&gt;\{
  31. (?:(?:[^{}]+)|(?2))+
  32. \})
  33. &quot;&quot;&quot;, re.M | re.X)
  34. rx_inner = re.compile(r&quot;interface ge-0/0/0.0;&quot;)
  35. for block in rx_block.finditer(data):
  36. m = rx_inner.search(block.group(&#39;body&#39;))
  37. if m:
  38. print(block.group(&#39;name&#39;))

<hr>

This would yield

  1. CPE-MGT

Two notes here: you wouldn't really need a regular expression for the inner search as it is a static string and two - probably use a parser solution instead.
See a demo for the expression on regex101.com.

huangapple
  • 本文由 发表于 2023年3月3日 20:03:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75626824.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定