sinrise
sinrise

Reputation: 391

Complex regex match everything between set of characters?

Regex! This isn't for a specific language. It's for a multi-file renamer that lets you use regex. So I'm just looking for a "pure" regex solution. I'm having trouble finding an answer that fits so I figured I'd ask.

Here is an example of the kinds of strings I'm working with:

[998FA551B]-[FIRE]-[#b.c.friends@fams]-[ My.Life.Story.V99A4.NONE.x4X5p-RIEP ]- my.life.story.v99a4.xrsr-riep

what I need is to remove everything that is not between '-[ ' and ' ]-' or 'dash, open bracket, space' and 'space, close bracket, dash'" and be left with:

My.Life.Story.V99A4.NONE.x4X5p-RIEP

Thank you!

Upvotes: 1

Views: 82

Answers (2)

Bohemian
Bohemian

Reputation: 425083

To match everything except the target:

^.*-\[ | \]-.*$

Then replace matches with a blank to delete.

See live demo of this regex working with your sample input.

Upvotes: 1

scrowler
scrowler

Reputation: 24405

You can use this regex to capture the name:

-\[\s(.+)\s\]-

Matched example: My.Life.Story.V99A4.NONE.x4X5p-RIEP

Breakdown:

-           # match the literal dash
\[          # match the opening square bracket (\ escape required)
\s          # match a single whitespace
(           # open capture group
    .+      # match anything at least once ()
)           # close capture group
\s          # match a single whitespace
\]          # match the closing square bracket (\ escape required)
-           # match the literal hash

Demo: http://regex101.com/r/fT7fI3

Upvotes: 1

Related Questions