用于匹配一组大括号之间的所有单词的正则表达式

编程入门 行业动态 更新时间:2024-10-25 19:22:44
本文介绍了用于匹配一组大括号之间的所有单词的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

对于我认识的大多数正则表达式专家来说,这是一个简单的问题,但我正在尝试返回句子中某些花括号之间的单词的所有匹配项;但是 Ruby 只返回一个匹配项,我无法弄清楚究竟是为什么.

A simple question for most regex experts I know, but I'm trying to return all matches for the words between some curly braces in a sentence; however Ruby is only returning a single match, and I cannot figure out why exactly.

我正在使用这个例句:

sentence = hello {name} is {thing}

使用此正则表达式尝试返回{name}"和{thing}":

with this regex to try and return both "{name}" and "{thing}":

sentence[/\{(.*?)\}/]

然而,Ruby 只返回{name}".谁能解释为什么这两个词都不匹配?

However, Ruby is only returning "{name}". Can anyone explain why it doesn't match for both words?

推荐答案

你很接近,但使用了错误的方法:

You're close, but using the wrong method:

sentence = "hello {name} is {thing}" sentence.scan(/\{(.*?)\}/) # => [["name"], ["thing"]]

更多推荐

用于匹配一组大括号之间的所有单词的正则表达式

本文发布于:2023-11-03 12:25:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555130.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:括号   单词   正则表达式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!