E
E
Emil Gimranov2021-07-05 09:08:31
JavaScript
Emil Gimranov, 2021-07-05 09:08:31

How to write regex without negative lookahead?

It is necessary to detect the following models: ShowMaker, Chapionships, Markerwons, Lee-Broks and All-Mars
The number is indicated as 4 digits. Sneaker sizes are presented in Russian and English sizes: (35-45)
RU and (5 to 11.5) UK. It is worth considering intermediate sizes, for example 36.5. It is worth considering that the model, series and number must be separated by a space or tab
For example: Chapionships 3285 7.5 UK or Lee-Broks 6832 39 RU

I wrote an approximate understanding of how it should be, but I could not split with a dot according to type 39.3
(ShowMaker|Chapionships| Markerwons|Lee-Broks|All-Mars)\s\d{0,4}\s(3([5-9]\d?)?|4([0-5]\d?)?).[ 0-9]\s(RU)

I think for English version 2 it's also easy

60e2a182836c7723587727.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2021-07-05
@Knorke

/(ShowMaker|Chapionships|Markerwons|Lee-Broks|All-Mars)\s(\d{4})\s((((([5-9]|10)(\.\d)?)|(11(\.[0-5])?))\sUK)|(((3[5-9]|4[0-4])(\.\d)?|45)\sRU))/

Test

(() => {
  const expression = /(ShowMaker|Chapionships|Markerwons|Lee-Broks|All-Mars)\s(\d{4})\s((((([5-9]|10)(\.\d)?)|(11(\.[0-5])?))\sUK)|(((3[5-9]|4[0-4])(\.\d)?|45)\sRU))/;

  const brands = ['ShowMaker', 'Chapionships', 'Markerwons', 'Lee-Broks', 'All-Mars'];

  const round = (number) => Math.round(number * 10) / 10;

  for (const brand of brands) {
    console.group(brand);
    console.group('UK');
    console.time();
    for (let size = 5; size <= 11.5; size += 0.1) {
      const variant = `${brand} 0000 ${round(size)} UK`;
      console.assert(expression.test(variant), variant);
    }
    console.timeEnd();
    console.groupEnd();

    console.group('RU');
    console.time();
    for (let size = 35; size <= 45; size += 0.1) {
      const variant = `${brand} 0000 ${round(size)} RU`;
      console.assert(expression.test(variant), variant);
    }
    console.timeEnd();
    console.groupEnd();
    console.groupEnd();
  }
})();

S
Sergey Sergey, 2021-07-05
@hahenty

Patterns inside a variadic group ( | ) do not require a question behind them.
The number with the question after [5-9] and [0-5] is not needed, right?
45.9 will go into the template.
For the British metrics, write separately, similar to the existing Russian template, and include metric templates through (|).
The British metric itself through brute-force options ([5-9]|10|11), tenths, and so on.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question