Regex match one of two words

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for Regex match one of two words

🍎🍌 Regex Magic: Matching Either "Apple" or "Banana" 🧙‍♀️

Welcome, tech enthusiasts! Today, we are going to unravel the enchanting world of regular expressions, taking on the challenge of matching either "apple" or "banana" from a given input. 🧐

Understanding the Problem:

The scenario is quite simple: we have an input where the value can only be either "apple" or "banana." Now, our mission is to cast a spell using regex, ensuring that only these two magic words are accepted. 🔮

🔑 Secret Recipe for Regex Success:

To crack this magical code, we need to harness the power of alternation in regex. The alternation operator is denoted by the pipe symbol | and acts as an "or" statement, allowing us to match either "apple" or "banana." 🎩

Sorcery Unleashed:

To achieve this spellbinding result, we need to compose our regex pattern accordingly.

^(apple|banana)$

Let's break down this enchanting incantation piece by piece:

  • ^ asserts the start of the input.

  • (apple|banana) represents the alternation group capturing either "apple" or "banana."

  • $ asserts the end of the input.

Examples of Magic in Action:

Now, let's put our magic to the test with some examples:

  1. Match:

    • apple
    • banana
  2. Don't Match:

    • orange
    • applepie
    • applebanana

Magical Tips and Tricks:

Here are a few additional magical tips and tricks to help you master your regex sorcery:

  1. Case Insensitivity: To perform a case-insensitive match, add the i flag at the end of the pattern.

    /^(apple|banana)$/i
  2. Matching on Word Boundaries: If you want to match only complete words "apple" or "banana," you can use the word boundary \b before and after the pattern.

    /\b(apple|banana)\b/

Time to Use Magic:

Now that you have unlocked the secrets of regex sorcery, it's time to put your new powers to the test! Run your spells on your favorite programming language, utilizing the appropriate regex functions or methods.

📣 Your Challenge:

We would love to see how you have mastered this magic spell! Share your experience in the comments below, along with any other regex conundrums you'd like us to unravel. Let's enchant the world of regex together! ✨✨

Remember, the power of regex is at your fingertips. With these newfound magical skills, you can conquer any pattern-matching problem that comes your way! 🚀

Happy coding and spell-casting! 🎩✨

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my