Your example uses a pattern_replace, which is a token filter. It operates on individual tokens in the text, rather than on the string as a whole. By the time the string i have 2 cars and 2cars gets to the filter, it's been transformed into a stream of tokens: ['i', 'have', '2', 'cars', 'and', '2cars']. The regex gets applied to each of those tokens, and 2 and cars don't match on their own, but the 2cars token does.
If you wanted 2 cars to be its own token, that's something that would be controlled by a "tokenizer" rather than a "token filter." For more details, see anatomy of an analyzer.
There is a lot that you can do with custom analyzers, but I'm not sure if I understand your use case fully. What is the problem you are trying to solve with this regular expression?
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.