rocky
rocky

Reputation: 113

How to write regex pattern in lucene?

I want to match a string from regexp query in lucene.

Test String:

       program-id.  acinstal.

Regex pattern in java:

^[a-z0-9 ]{6}[^*]\s*(program-id)\.

How would i write this regex specifically for lucene regexp query to match the string.

Upvotes: 5

Views: 29152

Answers (1)

femtoRgon
femtoRgon

Reputation: 33341

Two problems with your regex (assuming here, based on previous questions, that your test string is indexed without any tokenization. As a StringField, for instance):

  1. The regex must match a whole term. Without any analysis, as we're assuming, that means it must match the whole field. In this case, you need to add a .* to match the rest of the field

  2. Since you have to match the whole field anyway, anchors are not supported, so get rid of the ^ at the beginning.

So the regex that should work is:

[a-z0-9 ]{6}[^*]\s*(program-id)\..*

Upvotes: 4

Related Questions