Jack Stone
Jack Stone

Reputation: 37

Regex positive lookbehind

Let me apologize first. I've been fighting this SO editor for an hour. Sorry for the lousy formatting.

If I have a regex that matches a given input, then I put that regex into the positive look-behind wrapper, won't it still match the input it matched before?

For example, this input :

(NSString*)

will register a match with this regex:

\(\w*\*\)

I have confirmed this on gskinner.com. When I put that regex into the look-behind wrapper like so

(?<=\(\w*\*\))....

with this as the input:

(NSString*)help  

I do not receive the word help as a return.
This leads me to think I just plainly don't understand the look-behind concept. I watched a tutorial on this concept, but I am at a loss as to why this won't work. If I want to match:

(NSString*)

and return the next word, how can I go about that?

Upvotes: 1

Views: 1863

Answers (1)

Bohemian
Bohemian

Reputation: 425348

You have a space as the last character of the look behind, but your input has no space before "help". Also, there is no colon character before the input text, yet your look behind requires one.

Remove the space and the colon:

(?<=\(\w*\*\))\w+

Note that many regex engines disallow variable length look behinds, so a work around is to limit the.number of characters in the word to some large number, eg 99:

 (?<=\(\w{1,99}\*\))\w+

Upvotes: 1

Related Questions