By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,957 Members | 1,960 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,957 IT Pros & Developers. It's quick & easy.

Is 1 large Regex faster than 3 smaller ones?

P: n/a
Hi,

I need to match 3 small strings in a small text file (about 200 words
of text).

Would it be faster to write 1 compiled regex that matches all 3
substrings in one go, or to use 3 separate regular expressions to do
the same job?

Thanks!
Erik

Jun 3 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
In article <11**********************@p47g2000hsd.googlegroups .com>,
erikcw <er***********@gmail.comwrote:
Hi,

I need to match 3 small strings in a small text file (about 200 words
of text).

Would it be faster to write 1 compiled regex that matches all 3
substrings in one go, or to use 3 separate regular expressions to do
the same job?

Thanks!
Erik
For a classic regex, the answer is one big one. Matching against a regex
takes time proportional to the number of characters of input. One big
regex will probably consume more memory, and may be slower to compile, but
it should run faster.

On the other hand, there are a lot of things that pattern maching libraries
accept these days under the guise of being a "regex" which are not strictly
regexes in the classic sense.

From a purely practical point of view, if your input is only 200 words,
it's likely that the search time will be insignificant no matter what you
do.
Jun 3 '07 #2

P: n/a
On Jun 4, 4:52 am, erikcw <erikwickst...@gmail.comwrote:
Hi,

I need to match 3 small strings in a small text file (about 200 words
of text).

Would it be faster to write 1 compiled regex that matches all 3
substrings in one go, or to use 3 separate regular expressions to do
the same job?
Taking your question literally: we have no idea how long it might take
you to write alternative versions of source code.

Guessing that you mean "Which would run faster?": both should run over
a 200-word text file in imperceptibly different time (unless of course
you make a mistake), and the time should be tiny compared to the time
required to open the file (again unless of course you make a mistake).

I'd suggest that as a learning exercise you do both, check the
accuracy of both (text e.g. matching only 2 out of 3, sought strings
in different orders, etc), and then and only then worry about the time
it takes.

HTH,
John

Jun 3 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.