解析格式化字符串——隔离带引号的子字符串和花括号的 UUID [英] Parse a formatted string -- isolate quoted substring and curly braced UUID

查看:29
本文介绍了解析格式化字符串——隔离带引号的子字符串和花括号的 UUID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析具有唯一标识符的操作系统实例列表.我正在寻找一种解析文本字符串的解决方案,并将值传递给两个变量.要解析的字符串如下:

I am trying to parse a list of operating system instances with their unique identifiers. I am looking for a solution to parse a text string, and pass the values into two variables. The string to be parsed is as followed:

"Ubuntu 9.10" {40f2324d-a6b2-44e4-90c3-0c5fa82c987d}

推荐答案

我一直在找借口阅读 sscanf():

I've been looking for an excuse to read the docs for sscanf():

sscanf($s, '"%[^"]" {%[^}]}', $os, $ident);
echo $os, "<br>", $ident;

跟进:出于兴趣,目前关于此问题的三个答案中:

Followup: For interest's sake, out of the three answers currently on this question:

sscanf: 0.92999792098999 seconds
preg_match: 4.73761510849 seconds
str_replace x2 + preg_split: 3.7644839286804 seconds

此处为基准.有趣的是,两个 str_replace() 和一个 preg_split()preg_match() 更快.​​

Benchmark here. Funny that two str_replace() and a preg_split() are faster than the preg_match().

这篇关于解析格式化字符串——隔离带引号的子字符串和花括号的 UUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆