Regular expression to extract text between square brackets

ObiWanKenobi

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.

codaddict

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Regular expression to extract text between square brackets, comma and quote [] , "

Regular expression to extract specific text between brackets

Regular expression for text between double square brackets, but NOT between hyphens

Regular expression split string, extract string value before and numeric value between square brackets

How to extract text between square brackets in TSQL

java regular expression to extract content within square brackets

Java regular expression to extract content within square or round brackets

Regular Expression to remove everything but characters and numbers between Square brackets

Regular expression to catch start and end whitespaces between square brackets

regular expression for square brackets with dot

Selecting text between two square brackets with str_extract

sed on OS X - extract all text that is between square brackets

Regular expression to extract text between braces

Extract text between two markers with Regular Expression

remove text between square brackets

How to add links for text between brackets using regular expression

NP++: Regular expression to find square brackets

Regular expression to place number pair in square brackets

Java Regular expression matching square brackets

regular expression, multiple parentheses and square brackets

How to get square brackets with their elements with regular expression

Regular Expression to match numbers in square brackets in python

Regular expression find string outside square brackets

Regular expression - remove square brackets from results

How to describe square brackets in regular expression in XSD

How do I convert square brackets to curly bracket in text using by regular expression?

How to extract a string between square brackets

Extract last substring between square brackets

extract string between square brackets at second occurrence