正则表达式有什么用,从获取的wpa_supplicant.conf网络对象? [英] What regex use to get network object from wpa_supplicant.conf?
问题描述
我有以下结构文件:
ctrl_interface=/data/misc/wifi/sockets
driver_param=use_p2p_group_interface=1
update_config=1
device_name=P580_ROW
manufacturer=LENOVO
model_name=Lenovo
model_number=Lenov
serial_number=hjhjh7
device_type=10-0050F204-5
os_version=01020300
config_methods=physical_display virtual_push_button
p2p_no_group_iface=1
network={
ssid="test1"
psk="154695"
key_mgmt=WPA-PSK
sim_slot="-1"
imsi="none"
priority=1
}
network={
ssid="SSID2"
psk="test123456"
key_mgmt=WPA-PSK
sim_slot="-1"
imsi="none"
priority=19
}
我想问一下,我怎么可以使用正则表达式(Pattern和Matcher)或东西呈三角在Java中访问属性获取网络对象数组列表(但可以JSON太)?
I would like to ask, how can i get array list of the network objects (but can be JSON too) with accessible attributes using regex (Pattern and Matcher) or something simillar in Java?
我尝试使用服务来查找解决方案
http://txt2re.com/
但是,如果没有运气拿到的对象与访问属性。
I tried to find solution using the service http://txt2re.com/ But without luck to get objects with accessible attributes.
任何意见非常感谢。
推荐答案
如果您不知道属性的名字和他们的数量,你可以使用基于 \\ g的正则表达式
运营商:
If you do not know the names of attributes and their count, you can make use of a regex based on \G
operator:
String str = "<<YOUR_STRING>>";
Pattern ptrn = Pattern.compile("(?<new>network=\\{|(?!^)\\G)\\s*(?<key>\\w+)=\"?(?<value>[^\"\n]+)\"?");
Matcher matcher = ptrn.matcher(str);
int count = 1;
while (matcher.find()) {
if (matcher.group("new") != null && matcher.group("new").length() > 0) {
System.out.println("New Network: " + count);
count += 1;
}
System.out.println(matcher.group("key") + ":\"" + matcher.group("value").trim() + "\"");
}
请参阅 IDEONE演示
正则表达式 - | \\ s *(&LT?;键&GT; \\ w +)(LT;新&GT的网络= \\ {(^)\\ G变?!)?=(小于值&GT; [^\\ n] +)
- 匹配:
The regex - (?<new>network=\{|(?!^)\G)\s*(?<key>\w+)="?(?<value>[^"\n]+)"?
- matches:
-
(小于新&GT;?!网络= \\ {|(^)\\ G)
-网络= {
或previous成功的比赛结束(集团新) -
\\ S *
- 可选空白 -
(小于钥匙&GT; \\ w +)
- 1个或多个字母数字符号(集团密钥) -
=
- 字面=
和一个可选的 -
(小于值&GT; [^\\ n] +)
- 比其他1个或更多字符和换行符(集团值)
-
- 一个可选的
(?<new>network=\{|(?!^)\G)
-network={
or the end of the previous successful match (Group "new")\s*
- optional whitespace(?<key>\w+)
- 1 or more alphanumeric symbols (Group "key")="?
- literal=
and an optional"
(?<value>[^"\n]+)
- 1 or more characters other than"
and a newline (Group "value")"?
- an optional"
这篇关于正则表达式有什么用,从获取的wpa_supplicant.conf网络对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!