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

  • Home
  • SEARCH
  • 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 590825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:34:13+00:00 2026-05-13T15:34:13+00:00

How do I write a regular expression to match two given strings, at any

  • 0

How do I write a regular expression to match two given strings, at any position in the string?

For example, if I am searching for cat and mat, it should match:

The cat slept on the mat in front of the fire.
At 5:00 pm, I found the cat scratching the wool off the mat.

No matter what precedes these strings.

  • 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-13T15:34:14+00:00Added an answer on May 13, 2026 at 3:34 pm
    /^.*?\bcat\b.*?\bmat\b.*?$/m
    

    Using the m modifier (which ensures the beginning/end metacharacters match on line breaks rather than at the very beginning and end of the string):

    • ^ matches the line beginning
    • .*? matches anything on the line before…
    • \b matches a word boundary the first occurrence of a word boundary (as @codaddict discussed)
    • then the string cat and another word boundary; note that underscores are treated as “word” characters, so _cat_ would not match*;
    • .*?: any characters before…
    • boundary, mat, boundary
    • .*?: any remaining characters before…
    • $: the end of the line.

    It’s important to use \b to ensure the specified words aren’t part of longer words, and it’s important to use non-greedy wildcards (.*?) versus greedy (.*) because the latter would fail on strings like “There is a cat on top of the mat which is under the cat.” (It would match the last occurrence of “cat” rather than the first.)

    * If you want to be able to match _cat_, you can use:

    /^.*?(?:\b|_)cat(?:\b|_).*?(?:\b|_)mat(?:\b|_).*?$/m
    

    which matches either underscores or word boundaries around the specified words. (?:) indicates a non-capturing group, which can help with performance or avoid conflicted captures.

    Edit: A question was raised in the comments about whether the solution would work for phrases rather than just words. The answer is, absolutely yes. The following would match “A line which includes both the first phrase and the second phrase”:

    /^.*?(?:\b|_)first phrase here(?:\b|_).*?(?:\b|_)second phrase here(?:\b|_).*?$/m
    

    Edit 2: If order doesn’t matter you can use:

    /^.*?(?:\b|_)(first(?:\b|_).*?(?:\b|_)second|second(?:\b|_).*?(?:\b|_)first)(?:\b|_).*?$/m
    

    And if performance is really an issue here, it’s possible lookaround (if your regex engine supports it) might (but probably won’t) perform better than the above, but I’ll leave both the arguably more complex lookaround version and performance testing as an exercise to the questioner/reader.

    Edited per @Alan Moore’s comment. I didn’t have a chance to test it, but I’ll take your word for it.

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

Sidebar

Ask A Question

Stats

  • Questions 442k
  • Answers 442k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The cron job (and/or shebang) will allow you to specify… May 15, 2026 at 5:46 pm
  • Editorial Team
    Editorial Team added an answer This is a standard COS pointer. To locate it, just… May 15, 2026 at 5:46 pm
  • Editorial Team
    Editorial Team added an answer It's certainly possible. The Terminal basically just runs a shell… May 15, 2026 at 5:46 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.