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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:55:58+00:00 2026-05-25T09:55:58+00:00

I need a regular expression to get a word from a text input based

  • 0

I need a regular expression to get a word from a text input based on a index. Here the word should be in flowe brackets.

I will try to explain by giving an example.
If my input is hh{jhh}jkhjh{jkjhh{kljk}j}adajskjj}. For any character index, if it lies in between a pair of brackets I have to fetch that word including brackets.
In the above input if index is 2 or 4 output should be {jhh}.

  • 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-25T09:55:58+00:00Added an answer on May 25, 2026 at 9:55 am

    Try this. There’s a number of problems, of course. First, you didn’t say how you want to handle nested brackets. If you need to handle nested brackets, you’re out of luck with a simple regular expression, because you need a context-free grammar for that. Also note that the following implementation is naive in that it has linear time complexity on a word lookup (time to lookup a word is directly proportional to the number of matches found). That would only be a problem for large number of matches, however. Better implementation could use, for instance, a sorted array of matches and binary search for lookup.

    using System.Text.RegularExpressions;
    
    namespace WindowsFormsApplication1
    {
        public class ParsedInput
        {
            public static readonly Regex RegularExpression = new Regex("{[a-z]*}");
    
            private MatchCollection matches;
    
            public ParsedInput(string input)
            {
                matches = RegularExpression.Matches(input);
            }
    
            public bool TryGetWord(int index, out string word)
            {
                foreach (Match match in matches)
                {
                    if (index >= match.Index && index < (match.Index + match.Length))
                    {
                        word = match.Captures[0].Value;
                        return true;
                    }
                }
    
                word = "";
                return false;
            }
        }
    }
    

    To use, do:

    var parsed = new ParsedInput(input);
    string word = "";
    
    if (parsed.TryGetWord(index, out word))
    {
        // A word exists at the given index, do something.
    }
    else
    {
        // Handle missing word at the given index (optional).
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a regular expression to get the text out from between [ and
I need a regular expression to select all the text between two outer brackets.
I need a regular expression that will get digits only if theres no letters
I need a regular expression to basically get the first part of a string,
I would need to get a Regular Expression, which matches all Unicode control characters
How can I get content of a DIV using regular expression. What I need
I need a regular expression that will match the first letter of a song
I need a regular expression to extract from two types of URIs http://example.com/path/to/page/?filter http://example.com/path/to/?filter
I need a regular expression to get the Event, Name, School, Final Swim Time,
I need a regular expression that, given an ingredient line, will tell me the

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.