Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match Sequence using RegEx After a Specified Character

Tags:

regex

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!

like image 977
ninu Avatar asked May 26 '12 18:05

ninu


People also ask

How do I match a specific character in regex?

Match any specific character in a setUse square brackets [] to match any characters in a set. Use \w to match any single alphanumeric character: 0-9 , a-z , A-Z , and _ (underscore). Use \d to match any single digit. Use \s to match any single whitespace character.

How do you match a character after regex?

So essentially, the \s\S combination matches everything. It's similar to the dot character (.) which matches everything except new-lines. To specify how many characters we'd like to match after the character, we need to use a quantifier.

What does ?= Mean in regex?

?= is a positive lookahead, a type of zero-width assertion. What it's saying is that the captured match must be followed by whatever is within the parentheses but that part isn't captured. Your example means the match needs to be followed by zero or more characters and then a digit (but again that part isn't captured).

What is the regular expression matching one or more specific characters?

The character + in a regular expression means "match the preceding character one or more times". For example A+ matches one or more of character A. The plus character, used in a regular expression, is called a Kleene plus .


1 Answers

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 ]

like image 111
Bohemian Avatar answered Oct 13 '22 06:10

Bohemian