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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:15:49+00:00 2026-05-10T23:15:49+00:00

I’m trying to create a Regex usuable in C# that will allow me to

  • 0

I’m trying to create a Regex usuable in C# that will allow me to take a list of single letters and/or letter groups and ensure that a word is only comprised of items from that list. For instance:

  • ‘a’ would match ‘a’, ‘aa’, ‘aaa’, but not ‘ab’
  • ‘a b’ would match ‘a’, ‘ab’, ‘abba’, ‘b’, but not ‘abc’
  • ‘a b abc’ would match ‘a’, ‘ab’, ‘abc’, ‘aabc’, ‘baabc’, but not ‘ababac’

I thought something of the form

(a|b|abc)* 

would work, but it incorrectly matches the last term. Here’s the code I’m testing with:

[Fact] public void TestRegex() {     Regex regex = new Regex('(a|b|abc)*');     regex.IsMatch('a').ShouldBeTrue();     regex.IsMatch('b').ShouldBeTrue();     regex.IsMatch('abc').ShouldBeTrue();     regex.IsMatch('aabc').ShouldBeTrue();     regex.IsMatch('baabc').ShouldBeTrue();      // This should not match ... I don't think anyway     regex.IsMatch('ababac').ShouldBeFalse(); } 

I have a pretty basic understanding of regex, so apologies if I’m missing something obvious here 🙂

Update I don’t understand why your counter-example is a counter-example : ababac = a b a bac. cCould you clarify ?

I only want to use ‘a’, ‘b’, and ‘abc’ – ‘bac’ would be a completely different term.

Let me give another example: Using ‘ba’ and ‘t’, I could match the word ‘bat’, but not ‘tab’. The order of the letters inside the letter groups is important.

(Tests with Diadistis’ solution)

    [Fact]     public void TestRegex()     {         Regex regex = new Regex(@'\A(?:(e|l|ho)*)\Z');         regex.IsMatch('e').ShouldBeTrue();         regex.IsMatch('l').ShouldBeTrue();         regex.IsMatch('ho').ShouldBeTrue();         regex.IsMatch('elho').ShouldBeTrue();         regex.IsMatch('hole').ShouldBeTrue();         regex.IsMatch('holle').ShouldBeTrue();         regex.IsMatch('hello').ShouldBeFalse();         regex.IsMatch('hotel').ShouldBeFalse();     } 
  • 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. 2026-05-10T23:15:50+00:00Added an answer on May 10, 2026 at 11:15 pm

    I am not quite sure what are you trying to do but in order for the last one to be false you should check if the string can be matched entirely :

    Regex regex = new Regex(@'\A(?:(a|b|abc)*)\Z'); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.