# How to configure Rasa Regex to prefer longer match?

**URL:** <https://forum.rasa.com/t/how-to-configure-rasa-regex-to-prefer-longer-match/41408>\
**Category:** Rasa Open Source\
**Created:** [March 17, 2021, 8:58pm UTC](https://forum.rasa.com/t/how-to-configure-rasa-regex-to-prefer-longer-match/41408 "2021-03-17T20:58:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Talos0248](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/talos0248/32/13901_2.png) [@Talos0248](https://forum.rasa.com/u/Talos0248)\
**Post date:** [March 17, 2021, 8:58pm UTC](https://forum.rasa.com/t/how-to-configure-rasa-regex-to-prefer-longer-match/41408/1 "2021-03-17T20:58:26Z")

</div>

Hi there, I’m trying out using lookup tables to detect cities. In my **lookups table** , I have both “batu” (from Indonesia) and “batu caves” (from Malaysia) as examples. I’m using **RegexEntityExtractor** in my pipeline

Once the model is trained, I typed in: “ **What’s the weather in Batu Caves?** ”

However, the model decided to extract the entity “ **batu** ” instead of “ **batu caves** ”.

Is there a way to customize the regex behaviour to prefer longer matches?

---

<div class="post-metadata">

**Author:** ![Talos0248](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/talos0248/32/13901_2.png) [@Talos0248](https://forum.rasa.com/u/Talos0248)\
**Post date:** [March 18, 2021, 9:10am UTC](https://forum.rasa.com/t/how-to-configure-rasa-regex-to-prefer-longer-match/41408/2 "2021-03-18T09:10:48Z")

</div>

Update - Here is what my training data looks like:

![image](https://europe1.discourse-cdn.com/flex013/uploads/rasa/original/2X/f/f0eb9ef9ec66e908d557b7e8398f033ed8cbfb8b.png)

Inside my cities.yml:

![image](https://europe1.discourse-cdn.com/flex013/uploads/rasa/original/2X/f/f2ee09d8b585ddf5dd40e7c23505407f632088ae.png)

The ‘batu’ section I was referring to:

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/rasa/original/2X/4/4b578c96bfd9d237fee31f81cf6be03933fb3a18.png)

---

<div class="post-metadata">

**Author:** ![Talos0248](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/talos0248/32/13901_2.png) [@Talos0248](https://forum.rasa.com/u/Talos0248)\
**Post date:** [March 18, 2021, 12:11pm UTC](https://forum.rasa.com/t/how-to-configure-rasa-regex-to-prefer-longer-match/41408/3 "2021-03-18T12:11:09Z")

</div>

Update: I SOLVED IT! 😃

So what I did was sort my lookups in descending order such that the ones with the most number of characters were at the top of the list. Now regex featurizers automatically picks the ones with the longest characters!

It’s not your traditional workaround, but it somehow works. For someone curious to know how I managed to sort it in descending character length, here’s what I did:

1. I converted the lookup.yml file into a .csv file by changing the extension name
2. I opened up the .csv file in excel
3. I removed the **version** and **nlu: lookup:city examples:|** headers
4. I used [this](https://www.extendoffice.com/documents/excel/1338-excel-sort-by-length.html) method to sort the characters by length, deleted the length column, then saved the file
5. I saved the csv file, then renamed it to .yml
6. I re-added the headers with notepad and saved using UTF-8 format (since notepad++ gave me issues for some reason)
7. Popped the file back into the data folder, trained my nlu model, and everything works like a charm!

Hope this managed to help someone out!
