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 in some fast way if there is any text nodes
i need to check if a word are just with letter example hfjel return
I need to check if there exists an instance of class_A ,and if there
I need to check that some content exists on the page within a specific
I need to check some settings for all users on Windows clients in the
I need to check if my address has any words that matches word in
I need to check if some pictures (eps, tiff files) are in 300 DPI
I need part of a word. For example if we have CheckResults I just
I have text file. there around 1000 lines in it, each line contains a
In SQL Server, you need to check error for each SQL statement. For example

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.