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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:28:56+00:00 2026-05-29T20:28:56+00:00

I am working on a project that parses an incoming text file. I am

  • 0

I am working on a project that parses an incoming text file. I am learning C# as I go. My current method for picking out the information I need is something like:

string MyMatchString = @"a pattern to match";
Match MyMatch = Regex.Match(somestringinput, MyMatchString);
if (MyMatch.Succes)
{
   DoSomething(MyMatch.Value);
}

I am doing a lot of that. I’d like to be able to combine the match and the test for success in one step. Looking through the Class listings, Regex has an IsMatch() method, but it doesn’t appear that I can access the matched value (assuming it is successful). I think I need a Match instance for that. I tried

if ((Match MyMatch = Regex.Match(somestringinput, MyMatchString).Success)

but of course got a compile error.

I am thinking a static method that takes the match pattern and the input then returns a bool is the way to go. Then I can just test for success, and if so grab the matched value.

  • 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-29T20:28:58+00:00Added an answer on May 29, 2026 at 8:28 pm

    Well you can write an extension method for Regex which would give you some power. The trick is doing it without running the regex match twice, which could be a problem for your performance (note: this hasn’t been tested, and it differs from your idea in that it requires a ready-made Regex object to work).

    public static class RegexExtensions {
        public static bool GetMatch(this Regex regex, string input, out string matched) {
            Match match = regex.Match(input);
            if (match.Success) {
                matched = match.Value;
                return true;
            }
            matched = null;
            return false;
        }
    }
    

    So you’d do something like

    string matched;
    Regex regex = new Regex(pattern);
    if (regex.GetMatch(inputstring, matched))
    { /* do something with 'matched' */ }
    else
    { /* no match, 'matched' is null */ }
    

    You might prefer to just return null in the failure case and the string otherwise and dispense with the boolean and the output parameter.

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

Sidebar

Related Questions

I am working on a project that parses a text file thats suppose to
Working on a project that requires incoming email to be parsed, and certain information
I am working on a project to parse out a text file. The file
i am working on a section of a project, that parses Logs from Postgres
I'm working on a project that requires to convert html email into text. Below
I'm working on a project that downloads a zip file and unzips locally. The
I'm working in a project that need to get SVD (Single Values Decomposition) for
I have been working on a project that displays data in an XML file.
I'm working on a project that involves parsing a large csv formatted file in
I am working on a personal project that uses a custom config file. 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.