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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:34:35+00:00 2026-06-17T14:34:35+00:00

I have to write a regular expression to get three words from the text.

  • 0

I have to write a regular expression to get three words from the text. Words are separated with one space. And I wrote the code that gives me not all sequences.
For example for text “one two three four five six” I got only two sequences: 1.one two three 2.four five six. But I want my regular expression to give me all sequences so the output would be: 1.one two three 2.two three four 3.three four five. 4.four five six.
Can somebody tell me please what’s wrong with my regular expression?
Here is my code:

   string input = "one two three four five six";
   string pattern = @"([a-zA-Z]+ ){2}[a-zA-Z]+";
   Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
   MatchCollection matches = rgx.Matches(input);
   if (matches.Count > 0)
   {
       Console.WriteLine("{0} ({1} matches):", input, matches.Count);
       Console.WriteLine();
       foreach (Match match in matches)
           Console.WriteLine(match.Value);
   }
   Console.ReadLine();
  • 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-06-17T14:34:36+00:00Added an answer on June 17, 2026 at 2:34 pm

    There’s nothing wrong with your regular expression – it’s just how regular expressions work. When you find a match, the search for the next match continues at the end of the one you just found – the width of the match is consumed.

    So, how to fix this? One way is to make your match not consume anything. You can do this by placing your original pattern in a zero-width positive lookahead assertion:

    string pattern = @"(?=([a-zA-Z]+ ){2}[a-zA-Z]+)";
    added --->         ***                        * 
    

    (?=pattern) says “only match at this point if it’s immediately followed by soemthing matching pattern” – but the content matching pattern isn’t part of the overall match, so it isn’t consumed.

    If it’s not part of the match, though, it doesn’t appear in match.Value – so how do you get the value out? Simple – just add a capturing group around the original pattern (i.e. (?=(pattern))), and the captured group will appear in your results as normal.

    string pattern = @"(?=(([a-zA-Z]+ ){2}[a-zA-Z]+))";
    added --->            *                        *
    

    So now, you can go through your foreach loop as before, but match.Value will be empty – your desired result is in match.Groups[1].Value.

    But now you have another problem. Your results are

    one two three
    ne two three
    e two three
    two three four
    wo three four
    

    and so on. This is because your pattern matches even when you start halfway through a word.

    How to fix this?

    We add another zero-width assertion, this time a negative lookbehind: (?<![a-zA-Z]). Rather than saying “only match if this point is followed by the pattern”, it says “never match if this point is preceeded by the pattern”. Thus we’ll never match at a point preceeded by a letter. ne two three isn’t returned, for example, as it’s preceeded by o.

    string pattern = @"(?<![a-zA-Z])(?=(([a-zA-Z]+ ){2}[a-zA-Z]+))";
    added --->         *************
    

    With this pattern, you finally get your expected results.

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

Sidebar

Related Questions

I am trying to write a regular expression that can parse the text between
I am trying to get one regular expression that does the following: makes sure
I have the following regular expression: ^[[][A-Za-z_1-9]+[\]]$ I want to be able to get
I've been stuck trying to write this regular expression I need. Basically, I have
I try to write regular expression for Mongo DB query. I need to get
I have write this code but it does work only if I am already
I'm writing a simple web app in PHP that needs to have write access
I have to write a SNMP module which monitor a certain server application that
I have a text file that lists the names of a large number of
I am currently trying to write a regular expression to pull links out of

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.