正则表达式提取方括号之间的文本 [英] Regular expression to extract text between square brackets
本文介绍了正则表达式提取方括号之间的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
简单的正则表达式问题.我有以下格式的字符串:
Simple regex question. I have a string on the following format:
this is a [sample] string with [some] special words. [another one]
提取方括号内单词的正则表达式是什么,即.
What is the regular expression to extract the words within the square brackets, ie.
sample
some
another one
注意:在我的用例中,括号不能嵌套.
Note: In my use case, brackets cannot be nested.
推荐答案
您可以全局使用以下正则表达式:
You can use the following regex globally:
[(.*?)]
说明:
[
:[
是一个元字符,如果要按字面意思匹配,则需要对其进行转义.(.*?)
:以非贪婪的方式匹配所有内容并捕获它.]
:]
是一个元字符,如果要按字面意思匹配,则需要对其进行转义.
[
:[
is a meta char and needs to be escaped if you want to match it literally.(.*?)
: match everything in a non-greedy way and capture it.]
:]
is a meta char and needs to be escaped if you want to match it literally.
这篇关于正则表达式提取方括号之间的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文