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 6713841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:25:37+00:00 2026-05-26T08:25:37+00:00

Im trying to make a regular expression in python that allows me to find

  • 0

Im trying to make a regular expression in python that allows me to find a word within a string “n” times

For example, if i wanted to find a expression that could match if the word “cat” is exactly two times. How i would do that?

It should accept “The blue cat talks to the red cat in the tree”. Because it has “cat” exactly two times.

But it should not accept “The cat is big”. Because it has “cat” only once

And it should not accept either “the dog is yellow”. For similar reasons

Thanks a lot

EDIT
Hey guys

Sorry for complicating the problem too much, but i forgot to mention one thing.

If i wanted to find “cat” exactly two times, “The catcat runs” would also match

  • 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-26T08:25:38+00:00Added an answer on May 26, 2026 at 8:25 am

    Don’t use regular expressions just because they are there.

    words = text.split()
    print words.count('cat')
    

    As Vincent points out, that assumes all words are seperated by whitespace.

    words = re.findall("\b\w*") 
    

    Is probably a better options. Although whether that is neccesary depends on details not provided in your post.

    EDIT

    If you don’t even care about word boundaries, there is even less reason to be using a regular expression.

    print text.count("cat")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a regular expression for a string that has at
I'm trying to write a regular expression to match a string that may or
I'm trying to make a regular expression that accepts this: Only a-z, 0-9, _
I'm trying to use a regular expression to find all substrings that start with
I am trying to build a regular expression in javascript that checks for 3
I'm trying to make a regular expression testing for all quoted strings. It's okay.
I've got a regular expression that I'm trying to match against the following types
I am trying to make sense of handling regular expression with php. So far
Trying to make a make generic select control that I can dynamically add elements
Trying to make a custom :confirm message for a rails form that returns data

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.