Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7084217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:16:27+00:00 2026-05-28T07:16:27+00:00

If I need to check if for example a word A or word B

  • 0

If I need to check if for example a word A or word B exists in a text (String), is there a performance difference if I do:

if(text.contains(wordA) || text.contains(wordB))

to using some regular expression that searches the string?
Does it depend on the regular expression format?
Or is it just a matter of taste?

UPDATE:
If text.contains(wordA) is false then the text.contains(wordB) will be evaluated.
This means that contains will be called twice.

I was thinking if in performance terms a regex might be better than calling contains twice.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:16:27+00:00Added an answer on May 28, 2026 at 7:16 am

    With this trivial example you shouldn’t see much of a performance difference, but purely from the algorithms involved the regular expression

    wordA|wordB
    

    would indeed be faster, as it just makes a single pass through the string and employs a finite automaton to match one of the two substrings. However, this is offset by building the finite automaton first, which should be pretty much linear in the length of the regex in this case. You can compile the regex first to have that cost only once as long as the compiled object lives.

    So essentially cost comes down to:

    • linear search through the string twice (2 · string length)
    • or linear search through the string once and building the DFA (string length + regex length)

    if your text is very large and the substrings very small, then this could be worthwhile.

    Still, you’re optimising the wrong place, most likely. Use a profiler to find the actual bottlenecks in your code and optimise those; don’t ever worry about such trivial “optimisations” unless you can prove them to make an impact.

    One final thing to consider, though: With a regex you could make sure you’re actually matching words (or things that look like words) instead of word parts, which might be an actual reason to consider regex instead of contains.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to check a string to see if any word in it has
I need to check if a file exists on a server using delphi.. The
I need to check if a file exists on my server without using cache.
I need to check to see if a variable contains anything OTHER than a-z
I need to check various codes in a Word document. These codes are usually
I need part of a word. For example if we have CheckResults I just
I need to check for a string located inside a packet that I receive
I understand that some applications need to check against a server periodically, but why
First, I need to check the URL string, if the protocol of URL is
i want something like this the user enter a website link i need check

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.