使用正则表达式选择字符串的部分内容

我有两个字符串:

$ip =        [{"ip":"127.0.0.1"}]
$port =      [{"port":"80"}]

我想要保留第一个字符串中的 $ip = 127.0.0.1,第二个字符串中的 $port = 80。应该如何使用正则表达式实现?

//////////更新//////////

我现在这么写可以用了,但是写的不好:

   ip = string.match(respIp.body, "%:(.*)");
   ip = string.match(ip, "(.*)%}");
   ip = string.sub(ip, 2, string.len(ip)-1);

   port = string.match(respPort.body, "%:(.*)");
   port = string.match(port, "(.*)%}");
   port = string.sub(port, 2, string.len(port)-1);
点赞
用户7504558
用户7504558
`ip` - 匹配任何 `ip4` 格式的字符串,
`port` - 匹配双引号之间的任何十进制字符。
local bodyIp   = '$ip =        [{"ip":"127.0.0.1"}] '
local bodyPort = '$port =      [{"port":"80"}] '

local ip = bodyIp:match('(%d+%.%d+%.%d+%.%d+)') -- 任何 `ip`
local port = bodyPort:match(':"(%d+)"') -- 

print(ip,port)
2018-02-12 16:12:42
用户9050579
用户9050579

你可以使用 /([$]ip =).+(?<=:")([\d.]+)/g,然后获取第一组和第二组。

在线演示

但是,如果您只想获得另一个字符并将它们删除,请使用 (?:([\[\]\{\}\":]+)|(ip")|(port")|\s{2,})

演示

2018-02-12 16:41:45