ninu
ninu

Reputation: 920

Match Sequence using RegEx After a Specified Character

The initial string is [image:salmon-v5-09-14-2011.jpg]

I would like to capture the text "salmon-v5-09-14-2011.jpg" and used GSkinner's RegEx Tool

The closest I can get to my desired output is using this RegEx:

:([\w+.-]+)

The problem is that this sequence includes the colon and the output becomes

:salmon-v5-09-14-2011.jpg

How can I capture the desired output without the colon. Thanks for the help!

Upvotes: 37

Views: 111346

Answers (3)

Bohemian
Bohemian

Reputation: 424973

Use a look-behind:

(?<=:)[\w+.-]+

A look-behind (coded as (?<=someregex)) is a zero-width match, so it asserts, but does not capture, the match.

Also, your regex may be able to be simplified to this:

(?<=:)[^\]]+

which simply grabs anything between (but not including) a : and a ]

Upvotes: 81

agent-j
agent-j

Reputation: 27903

If you are always looking at strings in that format, I would use this pattern:

(?<=\[image:)[^\]]+

This looks behind for [image:, then matches until the closing ]

Upvotes: 6

Andrew White
Andrew White

Reputation: 53496

You have the correct regex only the tool you're using is highlighting the entire match and not just your capture group. Hover over the match and see what "group 1" actually is.

If you want a slightly more robust regex you could try :([^\]]+) which will allow for any characters other than ] to appear in the file name portion.

Upvotes: 1

Related Questions