开发者

Regular Expression to recognise truncated forms of search string?

开发者 https://www.devze.com 2022-12-29 02:37 出处:网络
I\'m trying to formulate a regular expression which will recognise the search term truncated by any nu开发者_JAVA技巧mber of characters from the right.

I'm trying to formulate a regular expression which will recognise the search term truncated by any nu开发者_JAVA技巧mber 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.


Due to the way regex works, yes, that's basically the most concise form.

0

精彩评论

暂无评论...
验证码 换一张
取 消