Regular Expression to recognise truncated forms of search string?

Posted by Moonshield on Stack Overflow See other posts from Stack Overflow or by Moonshield
Published on 2010-05-02T20:40:12Z Indexed on 2010/05/02 20:47 UTC
Read the original article Hit count: 195

Filed under:

I'm trying to formulate a regular expression which will recognise the search term truncated by any number of characters from the right.

For example, if the search term is "pickle", the regex should recognise "pi", "pick" but not "pickaxe".

Initially I came up with the following:

p(i(c(k(l(e)?)?)?)?)?

That works perfectly, but seems a crude way of doing it. Is there a better way of doing this? I had a look around for something similar to what I want, but I'm not entirely sure what to search for.

© Stack Overflow or respective owner

Related posts about regex