解析格式化字符串——隔离带引号的子字符串和大括号 UUID
我正在尝试解析操作系统实例及其唯一标识符的列表。我正在寻找一种解决方案来解析文本字符串,并将值传递给两个变量。待解析的字符串如下:
"Ubuntu 9.10" {40f2324d-a6b2-44e4-90c3-0c5fa82c987d}
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}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我一直在寻找借口阅读 sscanf() 的文档:
后续: 出于兴趣,目前对此问题有三个答案:
此处为基准。有趣的是,两个
str_replace()
和一个preg_split()
比preg_match()
更快。I've been looking for an excuse to read the docs for sscanf():
Followup: For interest's sake, out of the three answers currently on this question:
Benchmark here. Funny that two
str_replace()
and apreg_split()
are faster than thepreg_match()
.您可以使用正则表达式来匹配您需要的组:
您可以根据值缩小正则表达式的范围(例如第二个
.*
可以是[0-9a-f-]+
),但这就足够了。$matches[1]
将为“Ubuntu 9.10”,$matches[2]
将为“40f2324d-a6b2-44e4-90c3-0c5fa82c987d”You can use regular expressions to match the groups you need:
You can make the regular expression narrower based on the values (e.g. the second
.*
could be[0-9a-f-]+
), but that's sufficient.$matches[1]
will be "Ubuntu 9.10", and$matches[2]
will be "40f2324d-a6b2-44e4-90c3-0c5fa82c987d"这使用 str_replace 和 preg_split:
This gets the two strings as first and second elements of a string array using str_replace and preg_split: