【问题标题】:Get specific small string from a bigger one with multiple occurences从多次出现的较大字符串中获取特定的小字符串
【发布时间】:2017-05-19 18:57:52
【问题描述】:

我制作了一个 android 应用程序,它可以向控制台返回一些 Linux 命令。

在这种情况下,我想找到我的 wlan0 ip 和我的 wlan1(连接时)ip。 我得到了ifconfig 命令返回的全文。问题是如何才能进入这 2 个 IP 的 2 个变量? 我尝试在inet 单词之后添加子字符串,但inet 有超过 1 个单词。对此的最佳解决方案是什么?

这是我的字符串:

    lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 16436
    inet 127.0.0.1  netmask 255.0.0.0
    inet6 ::1  prefixlen 128  scopeid 0x10<host>
    loop  txqueuelen 0  (Local Loopback)
    RX packets 37  bytes 7123 (6.9 KiB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 37  bytes 7123 (6.9 KiB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

    p2p0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
    inet6 xxxxxx  prefixlen 64  scopeid 0x20<link>
    ether xxxxxx txqueuelen 1000  (Ethernet)
    RX packets 0  bytes 0 (0.0 B)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 0  bytes 0 (0.0 B)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

    wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
    inet 192.168.0.100  netmask 255.255.255.0  broadcast 192.168.0.255
    inet6 xxxxxxxxxxx  prefixlen 64  scopeid 0x20<link>
    ether xxxxxxx6  txqueuelen 1000  (Ethernet)
    RX packets 1504  bytes 817297 (798.1 KiB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 686  bytes 126080 (123.1 KiB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

请注意,我不关心 :p2p0lo 部分,只关心 wlan0 和 wlan1(连接时)。

我想要的是将 192.168.0.100 保存到 String ip 中;

提前致谢!

【问题讨论】:

  • 我发现我可以运行 ifconig wlan0 并且只返回 wlan0 部分。但是我怎么才能从整个文本中只获取 ip?

标签: java android linux string


【解决方案1】:

我不确定我是否理解您的尝试,但我认为Regular Expressions 是一种可能的方法

【讨论】:

  • 我想要的只是将“192.168.0.100”保存到一个变量中
猜你喜欢
  • 2011-10-06
  • 1970-01-01
  • 2011-04-03
  • 1970-01-01
  • 2020-02-26
  • 2017-05-28
  • 2013-11-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多