A
A
Anastasia2021-07-09 01:07:40
PHP
Anastasia, 2021-07-09 01:07:40

How to optimize the search for the amount before the word "ruble"?

Hello. There is a line:
Reservation No. 372 12343.22 dated June 24, 21. The cost of living is 1000.32 rubles. as well as a commission of 500 rubles.
I did this: /[0-9.]*.(?=руб.)/g, however regex101 says he did 579 steps. I understand this is a lot and probably the code can be optimized somehow. Tell me how?

I need to get 2 matches: 1000.32 and 500

Plus, my regular expression also gets a space after the sum. He's not needed

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dodo512, 2021-07-09
@nastya97core

\d+can be changed to \d++
\d++(?:\.\d+)?(?=\s*руб\.)
https://regex101.com/r/vxphzh/2
Or add (*SKIP)
\d+(*SKIP)(?:\.\d+)?(?=\s*руб\.)
https://regex101.com/r/vxphzh/3

J
John Didact, 2021-07-10
@JohnDidact

/(\d+(?:\.\d+)?) руб\./g
60e96ce718045868698847.jpeg
Although, perhaps, I missed something, I haven’t practiced for a long time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question