which of the golang regular expression (regexp string) packages is better

Asked 2 years ago, Updated 2 years ago, 330 views

If the regular expression of go is slow, there was an article on the Internet, so did you want to make it similar? Which is faster, string or regexp? (I didn't compare it with the code I told you because it was just similar.)

go regular-expression

2022-09-30 21:49

1 Answers

The string is probably faster.
The simple replacement process is The original text of Kafka's "transformation" is about 50 times faster

The reason why I said probably at the beginning is that the strings package and regexp package have about 3D features.
The former provides simple string manipulation, such as the Index function and Replace functions.
The latter offers a regular expression called "slow".

Therefore, if you use the former, you have to reinvent the functions necessary for regular expressions.
Self-made regular expression functions can be early or late depending on the producer's skill.
In extreme cases, if you implement regular expressions in exactly the same algorithm as regexp, the speed would be "slow."

Then, when asked if the regexp package is a useless regular expression, that's not the case.
It uses an algorithm called Thompson NFA that is different from regular expression packages such as Perl and Python, which is disadvantageous when the number of characters is small, but prevents exponential slowness.
The article has a distinctive graph of the former O(2^n) order and the latter O(n^2) order, which is an interesting.

Another caveat is that the regular expression is extremely slow to uncompiled, so it is important to use the regexp package in an appropriate implementation rather than simply making it a villain.

Reference: How to deal with regexp


2022-09-30 21:49

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.