简体   繁体   中英

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.

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)\..*

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM