Regex - Extract all text except square brackets and their contents

1k views Asked by At

I have a changelog that has the version numbers in square brackets like this:

## [0.3.0] - 2016-12-13 - FIX This is an example fix - FEATURE An example feature

I want to extract only the version numbers so I am left with:

[Unreleased] [0.3.0] [0.2.4] [0.2.3]

Ideally I'd like to extract only the first value in square brackets that isn't 'Unreleased' so I get the latest version number 0.3.0 although I'm not sure this is possible in regex.

Can anyone help? Thanks :)

2

There are 2 answers

2
Federico Piazza On BEST ANSWER

You can use a regex like this:

.*(\[.*?\]).*

With the replacement string: \1

Btw, if you want to remove all the other text, then you can use:

.*(\[.*?\]).*|*

Working demo

Update: if you want to transform the changelog in a list of versions, I could come up with this regex:

.*(\[.*?\])[\s\S]*?(^$|\z)

Working demo

0
SamWhan On

Edit (misread at first):

Replace

[\s\S]*?(\[[\d.]+])[\s\S]*

with

\1

Check out this example at regex101.