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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:02:08+00:00 2026-06-07T15:02:08+00:00

I am a newbie at regular expression. I have strings with formats like 1)

  • 0

I am a newbie at regular expression.
I have strings with formats like
1) 3.72 million people (country rank: 6th) (2004 estimate)
2) 10000 people (2007 estimate)

I would like extract the population number and the time from these two kinds of string. How can I do it in a regular expression in C#. Or do I need to write multiple regular expressions?

  • 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-07T15:02:09+00:00Added an answer on June 7, 2026 at 3:02 pm

    Here is a starting point:

    (?<population>\d\(.\d+)?)  #capturing group named "population"
                               #that is a number, optionally followed by a
                               #decimal point and at least one number
    \s*                        #followed by one or more spaces
    (?<magnitude>thousand|(m|b)illion)? #optional capturing group named "magnitude"
                                        # that matches "thousand", "million", or "billion"
    \s*                        #one or more whitespace characters
    people                     #the literal "people"
    .*                         #match any number of characters
    \(                         #Find literal opening parentheses...
       (?<year>\d{4})          #...followed by a four-digit year...
    \s                         #...followed by a space...
    estimate\)                 #...followed by the phrase "estimate)"
    \s*$                       #followed by optional whitespace
                               #and the end of the string
    

    A simple driver to show usage:

    class Program
    {
    /// Generate test strings
    static IEnumerable<string> Generator()
    {
        yield return "3.72 million people (country rank: 6th) (2004 estimate)";
        yield return "10000 people (2007 estimate)";
    }
    
    public static void Main()
    {
        string expression = @"
    (?<population>\d(.\d+)?)  #capturing group named 'population'
                               #that is a number, optionally followed by a
                               #decimal point and at least one number
    \s*                        #followed by one or more spaces
    (?<magnitude>thousand|(m|b)illion)? #optional capturing group named 'magnitude'
                                        # that matches 'thousand', 'million', or 'billion'
    \s*                        #one or more whitespace characters
    people                     #the literal 'people'
    .*                         #match any number of characters
    \(                         #Find literal opening parentheses...
       (?<year>\d{4})          #...followed by a four-digit year...
    \s                         #...followed by a space...
    estimate\)                 #...followed by the phrase 'estimate'
    \s*$                       #followed by optional whitespace
                               #and the end of the string";
    
        RegexOptions options = 
            RegexOptions.IgnorePatternWhitespace // allow whitespace/comments
            | RegexOptions.IgnoreCase
            | RegexOptions.ExplicitCapture; // Only capture named groups
    
        Regex r = new Regex(expression, options);
        foreach (var test in Generator())
        {
            Match match = r.Match(test);
            if (!match.Success)
                Console.WriteLine("Could not match {0}", test);
            else
            {
                double population = double.Parse(match.Groups["population"].Value);
                if (match.Groups["magnitude"].Success) // magnitude is optional
                                                       // but if present, need to
                                                       // multiply population
                {
                    switch (match.Groups["magnitude"].Value.ToLower())
                    {
                        case "thousand": population *= 1000; break;
                        case "million": population *= 1E6; break;
                        case "billion": population *= 1E9; break;
                        default: throw new FormatException("Unexpected value in magnitude group");
                    }
                }
                int year = int.Parse(match.Groups["year"].Value);
                Console.WriteLine("In {0}, population was {1} people.", year, population);
            }
        }
    }
    

    Outputs:

    In 2004, population was 3720000 people.
    In 2007, population was 10000 people.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am a regular expression newbie. I have a working code using nsregularexpression. i
I'm a newbie to regular expressions and I have the following string: sequence =
Newbie question. I have Django models that look like this: class Video(models.Model): uploaded_by =
I'm a regular expression newbie and I can't quite figure out how to write
i am sorry i am a newbie of regular expression. i searched this match
Sure it's a regular expression's newbie question, I saw it in a program but
I am newbie in ruby and regular expressions and I have this function write
I'm a newbie with regular expressions and i need some help :). I have
newbie here, so thanks in advance for help! I have a Wordpress site with
Newbie question... The objective: I intend to have an HTML text input field as

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.