Regular expression to extract text between square brackets
regex
Solution
You can use the following regex globally:
\[(.*?)\]
Explanation:
- `\[` : `[` 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.
Problem
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.
Related problems
- Find all string matches with Regex golang
- Regex lookahead, lookbehind and atomic groups
- Regular expression to match balanced parentheses
- Regular expression to get a string between two strings in Javascript
- How to remove square brackets in string using regex?
- How to match all occurrences of a regular expression in Ruby
- How to get only given captured group <regex> c++